gpt4 book ai didi

python - 如何打印谷歌搜索结果的数量(Beautifulsoup)

转载 作者:行者123 更新时间:2023-12-04 10:15:46 26 4
gpt4 key购买 nike

这是我到目前为止所做的事情:

import requests
from bs4 import BeautifulSoup

URL = "https://www.google.com/search?q=programming"
r = requests.get(URL)

soup = BeautifulSoup(r.content, 'html5lib')

table = soup.find('div', attrs = {'id':'result-stats'})

print(table)

我希望它获得整数形式的结果数,即 1350000000。

最佳答案

您缺少 header User-Agent,它是一个字符串,用于告诉服务器您正在使用哪种设备访问页面。

import requests
from bs4 import BeautifulSoup

headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36"}
URL = "https://www.google.com/search?q=programming"
result = requests.get(URL, headers=headers)

soup = BeautifulSoup(result.content, 'html.parser')

total_results_text = soup.find("div", {"id": "result-stats"}).find(text=True, recursive=False) # this will give you the outer text which is like 'About 1,410,000,000 results'
results_num = ''.join([num for num in total_results_text if num.isdigit()]) # now will clean it up and remove all the characters that are not a number .
print(results_num)

关于python - 如何打印谷歌搜索结果的数量(Beautifulsoup),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61064420/

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