簡單爬蟲可以劃分為get、post格式。其中,get是單方面的獲取資源,而post存在交互,如翻譯中需要文字輸入。本文主要描述簡單的get爬蟲。
環境準備
安裝第三方庫
pip install requests
pip install bs4
pip install lxml
進行爬蟲
1.獲取網頁數據。
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.解析網頁數據
soup = BeautifulSoup(response.text, 'lxml')
3.選取目標數據。此處key 依據源代碼目標標題的位置確定。首先進入開發者模式,后查看目標在html中的位置,右擊選擇“復制selector”,見下圖。

key = "#b_results > li > div.b_title > h2 > a"
soup.select(key)
4.清洗數據
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
總結
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關注腳本之家的更多內容!
您可能感興趣的文章:- Python大數據之網絡爬蟲的post請求、get請求區別實例分析
- python2與python3爬蟲中get與post對比解析
- python爬蟲 基于requests模塊的get請求實現詳解
- python爬蟲 基于requests模塊發起ajax的get請求實現解析
- python爬蟲中get和post方法介紹以及cookie作用