Python爬蟲(chóng)必備技巧詳細(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)

寫(xiě)入代碼
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)行失敗')
寫(xiě)在循環(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爬蟲(chóng)必備技巧詳細(xì)總結(jié)的文章就介紹到這了,更多相關(guān)Python 爬蟲(chóng)技巧內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)簡(jiǎn)單字典樹(shù)的方法
這篇文章主要介紹了Python實(shí)現(xiàn)簡(jiǎn)單字典樹(shù)的方法,實(shí)例分析了Python字典樹(shù)的定義、實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下2016-04-04
Python代碼實(shí)現(xiàn)找到列表中的奇偶異常項(xiàng)
這篇文章主要介紹了Python代碼實(shí)現(xiàn)找到列表中的奇偶異常項(xiàng),文章內(nèi)容主要利用Python代碼實(shí)現(xiàn)了從輸入列表中尋找奇偶異常項(xiàng),需要的朋友可以參考一下2021-11-11
Python測(cè)試WebService接口的實(shí)現(xiàn)示例
webService接口是走soap協(xié)議通過(guò)http傳輸,請(qǐng)求報(bào)文和返回報(bào)文都是xml格式的,本文主要介紹了Python測(cè)試WebService接口,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03
三大Python翻譯神器再也不用擔(dān)心學(xué)不好英語(yǔ)
本文介紹了用python做一款屬于自己的翻譯詞典軟件,從此告別網(wǎng)頁(yè)搜索,文中有非常詳細(xì)的代碼示例,小伙伴們快快行動(dòng)吧,需要的朋友可以參考下2021-09-09
Python實(shí)現(xiàn)緩存的兩個(gè)簡(jiǎn)單方法
緩存是一種用于提高應(yīng)用程序性能的技術(shù),它通過(guò)臨時(shí)存儲(chǔ)程序獲得的結(jié)果,以便在以后需要時(shí)重用它們,本文將學(xué)習(xí)Python中的不同緩存技術(shù),感興趣的可以了解下2024-11-11
Pytorch中torch.argmax()函數(shù)使用及說(shuō)明
這篇文章主要介紹了Pytorch中torch.argmax()函數(shù)使用及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
Python使用combinations實(shí)現(xiàn)排列組合的方法
今天小編就為大家分享一篇Python使用combinations實(shí)現(xiàn)排列組合的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
python使用socket實(shí)現(xiàn)圖像傳輸功能
這篇文章主要為大家詳細(xì)介紹了python使用socket實(shí)現(xiàn)圖像傳輸功能,linux服務(wù)器與win10主機(jī)間的圖像傳輸,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06
python實(shí)現(xiàn)定時(shí)發(fā)送郵件
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)定時(shí)發(fā)送郵件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12

