gpt4 book ai didi

python - 使用 BeautifulSoup 防止被禁止进行 google 抓取

转载 作者:行者123 更新时间:2023-12-01 06:50:20 25 4
gpt4 key购买 nike

我想使用 PythonBeautifulSoup 制作 Google 新闻 scraper,但我了解到我有可能被禁止。

我还了解到,我可以通过使用一些轮换代理和轮换 IP 地址来防止这种情况。我唯一能做的就是旋转User-Agent。您能告诉我如何添加轮换代理和轮换 IP 地址吗?

我知道它应该添加到 request.get() 部分,但我不知道如何添加。

这是我的代码:

from bs4 import BeautifulSoup
import requests

headers = {'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36'}

term = 'usa'
page=0

for page in range(1,5):

page = page*10

url = 'https://www.google.com/search?q={}&tbm=nws&sxsrf=ACYBGNTx2Ew_5d5HsCvjwDoo5SC4U6JBVg:1574261023484&ei=H1HVXf-fHfiU1fAP65K6uAU&start={}&sa=N&ved=0ahUKEwi_q9qog_nlAhV4ShUIHWuJDlcQ8tMDCF8&biw=1280&bih=561&dpr=1.5'.format(term,page)
print(url)

response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')

headline_text = soup.find_all('h3', class_= "r dO0Ag")

snippet_text = soup.find_all('div', class_='st')

news_date = soup.find_all('div', class_='slp')

print(len(news_date))

最佳答案

您可以使用 Google 提供的适当 API 进行搜索:

https://developers.google.com/custom-search/v1/overview

关于python - 使用 BeautifulSoup 防止被禁止进行 google 抓取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59047268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com