【发布时间】:2023-03-17 10:02:02
【问题描述】:
您好 Stack Overflow 贡献者!
我想抓取一个新闻网站的多个页面;它在此步骤中显示错误消息
response = requests.get(page, headers = user_agent)
错误信息是
AttributeError: 'int' object has no attribute 'get'
代码行是
user_agent = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; Touch; rv:11.0) like Gecko'}
#controlling the crawl-rate
start_time = time()
request = 0
def scrape(url):
urls = [url + str(x) for x in range(0,10)]
for page in urls:
response = requests.get(page, headers = user_agent)
print(page)
print(scrape('https://nypost.com/search/China+COVID-19/page/'))
更具体地说,这个页面和它旁边的页面是我要抓取的:
https://nypost.com/search/China+COVID-19/page/1/?orderby=relevance
任何帮助将不胜感激!
【问题讨论】:
-
您很可能在代码中的其他位置使用整数值定义了
requests。 -
添加到@Shreya 评论,如果您使用 python3.6+ f'{'Request:{attempts}; ,则必须将变量 request 更改为尝试并使用 f 字符串代替格式频率:{request/elapsed_time} request/s'}'
-
真的!我在其他地方定义了请求;删除后,它可以工作
标签: python for-loop web-scraping beautifulsoup fetch