Python爬蟲必備技巧詳細(xì)總結(jié)
自定義函數(shù)
import requests from bs4 import BeautifulSoup headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0'} def baidu(company): url = 'https://www.baidu.com/s?rtt=4&tn=news&word=' + company print(url) html = requests.get(url, headers=headers).text s = BeautifulSoup(html, 'html.parser') title=s.select('.news-title_1YtI1 a') for i in title: print(i.text) # 批量調(diào)用函數(shù) companies = ['騰訊', '阿里巴巴', '百度集團(tuán)'] for i in companies: baidu(i)
批量輸出多個(gè)搜索結(jié)果的標(biāo)題
結(jié)果保存為文本文件
import requests from bs4 import BeautifulSoup headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0'} def baidu(company): url = 'https://www.baidu.com/s?rtt=4&tn=news&word=' + company print(url) html = requests.get(url, headers=headers).text s = BeautifulSoup(html, 'html.parser') title=s.select('.news-title_1YtI1 a') fl=open('test.text','a', encoding='utf-8') for i in title: fl.write(i.text + '\n') # 批量調(diào)用函數(shù) companies = ['騰訊', '阿里巴巴', '百度集團(tuán)'] for i in companies: baidu(i)
寫入代碼
fl=open('test.text','a', encoding='utf-8') for i in title: fl.write(i.text + '\n')
異常處理
for i in companies: try: baidu(i) print('運(yùn)行成功') except: print('運(yùn)行失敗')
寫在循環(huán)中 不會(huì)讓程序停止運(yùn)行 而會(huì)輸出運(yùn)行失敗
休眠時(shí)間
import time for i in companies: try: baidu(i) print('運(yùn)行成功') except: print('運(yùn)行失敗') time.sleep(5)
time.sleep(5)
括號(hào)里的單位是秒
放在什么位置 則在什么位置休眠(暫停)
爬取多頁(yè)內(nèi)容
百度搜索騰訊
切換到第二頁(yè)
去掉多多余的
https://www.baidu.com/s?wd=騰訊&pn=10
分析出
https://www.baidu.com/s?wd=騰訊&pn=0 為第一頁(yè)
https://www.baidu.com/s?wd=騰訊&pn=10 為第二頁(yè)
https://www.baidu.com/s?wd=騰訊&pn=20 為第三頁(yè)
https://www.baidu.com/s?wd=騰訊&pn=30 為第四頁(yè)
..........
代碼
from bs4 import BeautifulSoup import time headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0'} def baidu(c): url = 'https://www.baidu.com/s?wd=騰訊&pn=' + str(c)+'0' print(url) html = requests.get(url, headers=headers).text s = BeautifulSoup(html, 'html.parser') title=s.select('.t a') for i in title: print(i.text) for i in range(10): baidu(i) time.sleep(2)
到此這篇關(guān)于Python爬蟲必備技巧詳細(xì)總結(jié)的文章就介紹到這了,更多相關(guān)Python 爬蟲技巧內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python猜解網(wǎng)站數(shù)據(jù)庫(kù)管理員密碼的腳本
這篇文章主要和大家分享一個(gè)Python腳本,可以實(shí)現(xiàn)猜解網(wǎng)站數(shù)據(jù)庫(kù)管理員的密碼。文中的示例代碼講解詳細(xì),需要的小伙伴可以參考一下2022-02-02Python連接HDFS實(shí)現(xiàn)文件上傳下載及Pandas轉(zhuǎn)換文本文件到CSV操作
這篇文章主要介紹了Python連接HDFS實(shí)現(xiàn)文件上傳下載及Pandas轉(zhuǎn)換文本文件到CSV操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06Python餐飲AI機(jī)器人學(xué)習(xí)數(shù)據(jù)網(wǎng)絡(luò)抓取
在餐飲行業(yè),AI機(jī)器人可以通過(guò)學(xué)習(xí)大量的相關(guān)數(shù)據(jù)來(lái)提供更好的服務(wù)和體驗(yàn),在文本中,我們將介紹如何使用python進(jìn)行餐飲A?I機(jī)器人學(xué)習(xí)數(shù)據(jù)的網(wǎng)絡(luò)抓取,并提供代碼的示例和最佳實(shí)踐2023-12-12Python中eval帶來(lái)的潛在風(fēng)險(xiǎn)代碼分析
這篇文章主要介紹了Python中eval帶來(lái)的潛在風(fēng)險(xiǎn)代碼分析,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12只用40行Python代碼就能寫出pdf轉(zhuǎn)word小工具
今天咱們介紹一個(gè)pdf轉(zhuǎn)word的免費(fèi)小工具,滿足這么一個(gè)不常見(jiàn)但是偶爾會(huì)出來(lái)煩人的需求文中有非常詳細(xì)的代碼示例,對(duì)小伙伴們很有幫助,需要的朋友可以參考下2021-05-05PyTorch實(shí)現(xiàn)圖像識(shí)別實(shí)戰(zhàn)指南
圖像識(shí)別是從給定圖像中提取有意義的信息(例如圖像內(nèi)容)的過(guò)程,下面這篇文章主要給大家介紹了關(guān)于PyTorch實(shí)現(xiàn)圖像識(shí)別的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02