来自usnews.com的python请求超时其他网站工作正常

来自usnews.com的python请求超时其他网站工作正常,python,python-requests,Python,Python Requests,来自usnews.com的请求运行不正常。代码将永远运行,或按说明在五秒钟后超时。我曾经尝试过使用其他工作非常好的网站(wikipedia.org,google.com)。他们使用了一种特殊的保护措施,以防像你这样的网络爬虫。每当您访问网站时,您的web浏览器都会发送一条称为用户代理的特殊数据。它告诉网站你正在使用什么类型的浏览器,如果你在电话或电脑上。默认情况下,请求模块不执行此操作 您可以非常轻松地设置自己的用户代理。以您的网站为例: url = "https://www.usne

来自
usnews.com
请求运行不正常。代码将永远运行,或按说明在五秒钟后超时。我曾经尝试过使用其他工作非常好的网站(wikipedia.org,google.com)。

他们使用了一种特殊的保护措施,以防像你这样的网络爬虫。每当您访问网站时,您的web浏览器都会发送一条称为
用户代理的特殊数据。它告诉网站你正在使用什么类型的浏览器,如果你在电话或电脑上。默认情况下,
请求
模块不执行此操作

您可以非常轻松地设置自己的
用户代理。以您的网站为例:

url = "https://www.usnews.com"
page = requests.get(url, timeout = 5)
soup = BeautifulSoup(page.content,"html.parser")
这个代码告诉网站,我们是一个真实的人,而不是一个机器人


您可以在此处()了解有关用户代理的更多信息。

您应该尝试类似于selenium的内容。此代码类似于selenium,但有点用户友好

import requests
from bs4 import BeautifulSoup

headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36"}

url = "https://www.usnews.com"
page = requests.get(url, headers=headers)
soup = BeautifulSoup(page.content,"html.parser")

此代码模拟真实的搜索引擎,应绕过机器人检测

该站点可能被防火墙或其他任何原因阻止。请查看robots.txt文件()。此文件通常描述bot访问的权限。
from requests_html import HTMLSession
import re
#from fake_useragent import UserAgent
#create the session
#ua = UserAgent()
session = HTMLSession()

#define our URL
url = "https://www.usnews.com"

#use the session to get the data
r = session.get(url)

#Render the page, up the number on scrolldown to page down multiple times on a page
r.html.render(sleep=1,timeout = 30, keep_page=True, scrolldown=1)

print(r.text)