最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python爬蟲必備技巧詳細(xì)總結(jié)

 更新時間:2021年10月22日 08:39:18   作者:小旺不正經(jīng)  
本篇文章介紹了我在爬蟲過程中總結(jié)的幾個必備技巧,都是經(jīng)過實(shí)驗(yàn)的,通讀本篇對大家的學(xué)習(xí)或工作具有一定的價值,需要的朋友可以參考下

自定義函數(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)

批量輸出多個搜索結(jié)果的標(biāo)題

image-20211021110056346

結(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)

image-20211021111507772

寫入代碼

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)中 不會讓程序停止運(yùn)行 而會輸出運(yùn)行失敗

休眠時間

import time
for i in companies:
    try:
        baidu(i)
        print('運(yùn)行成功')
    except:
        print('運(yùn)行失敗')
time.sleep(5)

time.sleep(5)

括號里的單位是秒

放在什么位置 則在什么位置休眠(暫停)

爬取多頁內(nèi)容

百度搜索騰訊

image-20211021115808442

切換到第二頁

image-20211021115857316

去掉多多余的

https://www.baidu.com/s?wd=騰訊&pn=10

分析出

https://www.baidu.com/s?wd=騰訊&pn=0 為第一頁
https://www.baidu.com/s?wd=騰訊&pn=10 為第二頁
https://www.baidu.com/s?wd=騰訊&pn=20 為第三頁
https://www.baidu.com/s?wd=騰訊&pn=30 為第四頁
..........

代碼

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)

image-20211021130805642

到此這篇關(guān)于Python爬蟲必備技巧詳細(xì)總結(jié)的文章就介紹到這了,更多相關(guān)Python 爬蟲技巧內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

长岭县| 公安县| 平江县| 阿克苏市| 黄骅市| 武汉市| 来安县| 日喀则市| 肇庆市| 囊谦县| 和林格尔县| 朝阳市| 镇康县| 深水埗区| 元阳县| 乌海市| 绥滨县| 衡南县| 建瓯市| 嘉祥县| 象山县| 周宁县| 北海市| 肇庆市| 新干县| 磴口县| 新源县| 绵阳市| 蒙城县| 于都县| 鸡西市| 汪清县| 江油市| 兴城市| 元氏县| 织金县| 什邡市| 中牟县| 盐池县| 莱芜市| 临洮县|