首页 > 解决方案 > 如何在 Python 中向网站发送多个 get 请求?

问题描述

我在计算机科学实验室 (LIRIS) 实习了 3 个月。我的实习主管让我在meilleurs-agents.com上检索一些数据。这是一个房地产网站,我想检索每个城市的平方米价格。我的程序是用 Python 编写的,我实际上尝试发送多个请求来获取数据。但由于代理错误,它不起作用:

HTTPConnectionPool(host='XXXXXX', port=XXXX): Max retries exceeded with url: "..." (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000000000B304320>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed',)))

我的代码预览:

headers = requests.utils.default_headers()
headers.update({
    'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'
})  
for city, postal_code in zip(cities, postal_codes):
    url = 'https://www.meilleursagents.com/prix-immobilier/'+city+'-'+postal_code+'/'

    PROXY = {'https' : 'XX.XXX.X.XXX:XXXX'}

    try:
        response = requests.get(url, timeout=10, proxies=PROXY)
    except Exception as e :
        print(e)

如果我删除代理,我的请求有效,但 html 代码包含一条消息,例如“你似乎是一个机器人,所以你的请求尚未完成”,所以我无法获得价格......但我真的需要这些数据

希望我的问题很清楚,有人可以帮助我:)

谢谢,耐莉

PS:对不起我的英语,我是法国学生:D

标签: pythonproxypython-requests

解决方案


User-Agent尝试为您的请求更改标头和 cookie。

另一种解决方法是尝试在请求之间添加一些超时:

time.sleep(1)  # try to use different time values

这当然会减慢您的脚本速度,但可能有助于避免过多请求错误。


推荐阅读