gpt4 book ai didi

python - 页面内容根据每个请求而变化

转载 作者:行者123 更新时间:2023-12-01 00:46:05 25 4
gpt4 key购买 nike

我正在尝试从存储有关法国公司的各种信息的网站上抓取 HTML 页面。然而,每次我通过 pd.read_html 发送请求(仅提取表格)时,我都会得到不同的结果。

为了说明我的观点,您将找到一些可以重现的代码:

result = []
for i in range(0,10):
result.extend(pd.read_html('https://www.societe.com/societe/eram-388583239.html', encoding='utf-8',attrs={'id':'rensjur'}))
time.sleep(5)
print(result)

我希望得到与在浏览器中打开链接时相同的表格。

最佳答案

尝试了几分钟后,我发现更改用户代理会有所帮助。我的猜测是,当网站检测到不是网络浏览器的用户代理时,它会混淆真实数据。

我确信有一种更优雅的方法可以做到这一点,但这是我使用的每次提取相同数据的代码:

import pandas as pd
import time
import urllib.request as request

results = []
for i in range(0,10):
url = 'https://www.societe.com/societe/eram-388583239.html'
opener = request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
response = opener.open(url)
result = pd.read_html(response.read(), encoding='utf-8',attrs={'id':'rensjur'})
print(result)
results.extend(result)
time.sleep(5)
print(results)

关于python - 页面内容根据每个请求而变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56973073/

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