目錄
- 環(huán)境準(zhǔn)備
- 進(jìn)行爬蟲
- 參考
- 總結(jié)
簡(jiǎn)單爬蟲可以劃分為get、post格式。其中,get是單方面的獲取資源,而post存在交互,如翻譯中需要文字輸入。本文主要描述簡(jiǎn)單的get爬蟲。
環(huán)境準(zhǔn)備
安裝第三方庫
pip install requests
pip install bs4
pip install lxml
進(jìn)行爬蟲
1.獲取網(wǎng)頁數(shù)據(jù)。
import requests
from bs4 import BeautifulSoup
url = "https://cn.bing.com/search?q=爬蟲CSDNqs=nform=QBREsp=-1pq=爬蟲csdnsc=5-6sk=cvid=0B13B88D8F444A0182A4A6C36E463179/"
response = requests.get(self.url)
2.解析網(wǎng)頁數(shù)據(jù)
soup = BeautifulSoup(response.text, 'lxml')
3.選取目標(biāo)數(shù)據(jù)。此處key 依據(jù)源代碼目標(biāo)標(biāo)題的位置確定。首先進(jìn)入開發(fā)者模式,后查看目標(biāo)在html中的位置,右擊選擇“復(fù)制selector”,見下圖。
key = "#b_results > li > div.b_title > h2 > a"
soup.select(key)
4.清洗數(shù)據(jù)
result = {}
for i, item in enumerate(data):
result.update({
f'title_{i}': item.get_text(),
f'url_{i}': item.get('href')
})
print(result)
參考
鏈接:https://www.jb51.net/article/152560.htm
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
您可能感興趣的文章:- Python大數(shù)據(jù)之網(wǎng)絡(luò)爬蟲的post請(qǐng)求、get請(qǐng)求區(qū)別實(shí)例分析
- python2與python3爬蟲中g(shù)et與post對(duì)比解析
- python爬蟲 基于requests模塊的get請(qǐng)求實(shí)現(xiàn)詳解
- python爬蟲 基于requests模塊發(fā)起ajax的get請(qǐng)求實(shí)現(xiàn)解析
- python爬蟲中g(shù)et和post方法介紹以及cookie作用