gpt4 book ai didi

Python 通过 'requests' 获取 HTML 内容返回部分响应

转载 作者:行者123 更新时间:2023-11-30 21:49:41 35 4
gpt4 key购买 nike

我正在使用以下 3 行内容阅读网站内容。我使用了一个待售示例域,但内容不多。

url = "http://localbusiness.com/"
response = requests.get(url)
html = response.text

当您查看源代码时,它会返回以下 html 内容,其中网站包含更多 html。我在这里做错了什么

Python 版本 2.7

<html><head></head><body><!-- vbe --></body></html>

最佳答案

尝试设置用户代理:

import requests

url = "http://localbusiness.com/"

headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36',
'Content-Type': 'text/html',
}

response = requests.get(url, headers=headers)
html = response.text

请求设置的默认User-Agent'User-Agent': 'python-requests/2.8.1'。尝试模拟请求来自浏览器而不是脚本。

关于Python 通过 'requests' 获取 HTML 内容返回部分响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33755849/

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