日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

网络爬虫练习

發(fā)布時間:2023/12/9 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 网络爬虫练习 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.利用requests.get(url)獲取網(wǎng)頁頁面的html文件

import requests newsurl='http://news.gzcc.cn/html/xiaoyuanxinwen/' res = requests.get(newsurl) #返回response對象 res.encoding='utf-8' print(res.text)

?

2.利用BeautifulSoup的HTML解析器,生成結(jié)構(gòu)樹

import requests from bs4 import BeautifulSoup newsurl='http://news.gzcc.cn/html/xiaoyuanxinwen/' res = requests.get(newsurl) #返回response對象 res.encoding='utf-8' print(BeautifulSoup(res.text,'html.parser'))

3.找出特定標(biāo)簽的html元素

soup.p #標(biāo)簽名,返回第一個

soup.head

soup.p.name #字符串

soup.p. attrs #字典,標(biāo)簽的所有屬性

soup.p. contents # 列表,所有子標(biāo)簽

soup.p.text #字符串

soup.p.string

soup.select(‘li')

?

4.取得含有特定CSS屬性的元素

soup.select('#p1Node')

soup.select('.news-list-title')

?

5.練習(xí):

取出h1標(biāo)簽的文本

import requests from bs4 import BeautifulSoup url = 'http://news.gzcc.cn/html/xiaoyuanxinwen/' res = requests.get(url) res.encoding = 'utf-8' soup = BeautifulSoup(res.text,'html.parser') #取h4標(biāo)簽的文本 print(soup.select('h4')[0].text)

?

取出a標(biāo)簽的鏈接

import requests from bs4 import BeautifulSoup url = 'http://news.gzcc.cn/html/xiaoyuanxinwen/' res = requests.get(url) res.encoding = 'utf-8' soup = BeautifulSoup(res.text,'html.parser') #取a標(biāo)簽的鏈接 for i in soup.select('a'):print(i.get('href'))

?

取出所有l(wèi)i標(biāo)簽的所有內(nèi)容

import requests from bs4 import BeautifulSoup url = 'http://news.gzcc.cn/html/xiaoyuanxinwen/' res = requests.get(url) res.encoding = 'utf-8' soup = BeautifulSoup(res.text,'html.parser') #取li標(biāo)簽的所有內(nèi)容 for i in soup.select('li'):print(i)

?


取出一條新聞的標(biāo)題、鏈接、發(fā)布時間、來源

import requests from bs4 import BeautifulSoup url = 'http://news.gzcc.cn/html/xiaoyuanxinwen/' res = requests.get(url) res.encoding = 'utf-8' soup = BeautifulSoup(res.text,'html.parser')# 取出一條新聞的標(biāo)題、鏈接、發(fā)布時間、來源 print(soup.select('.news-list-title')[0].text) print(soup.select('li')[1].a.attrs['href']) print(soup.select('.news-list-info')[0].contents[0].text) print(soup.select('.news-list-info')[0].contents[1].text)

?

轉(zhuǎn)載于:https://www.cnblogs.com/weixingna/p/8670551.html

總結(jié)

以上是生活随笔為你收集整理的网络爬虫练习的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。