【发布时间】:2021-07-09 14:02:38
【问题描述】:
我正在尝试使用 Python 中的请求库来抓取网站数据,并以 429 Client Error: Too Many Requests for URL 结束,而我什至没有以编程方式访问过 URL。
需要帮助来克服这个错误,在此先感谢。
下面是代码:
import requests
import json
if __name__ == '__main__':
BASE_URL = f"https://groww.in/mutual-funds"
LISTING_URL = f"https://groww.in/slr/v1/search/derived/scheme"
HEADERS = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, '
'like Gecko) '
'Chrome/80.0.3987.149 Safari/537.36',
'accept-language': 'en,gu;q=0.9,hi;q=0.8', 'accept-encoding': 'gzip, deflate, br'}
PARAMS = {'available_for_investment': 'true', 'doc_type': 'scheme', 'page': 0, 'plan_type': 'Direct',
'size': 16, 'sort_by': 0}
try:
session = requests.Session()
print('FETCHING & SETTING COOKIES...')
request = session.get(BASE_URL, headers=HEADERS, timeout=20)
cookies = dict(request.cookies)
response = session.get(url=LISTING_URL, headers=HEADERS, params=PARAMS, timeout=20,
cookies=cookies)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
raise SystemExit(err)
dajs = json.loads(response.text)
【问题讨论】:
-
您尝试过什么解决问题的方法?你被困在哪里了?也许网站不喜欢被刮一次?
-
@NicoHaase 最初我尝试使用没有 cookie 的普通 requests.get() 然后尝试使用 cookie 的会话方法。两者都给出相同的错误。
-
@BeingSuman,我猜 LISTING_URL 有问题 bcoz url 甚至无法从浏览器访问并抛出相同的 429 错误。
-
@Shivam 您无法直接访问 LISTING_URL,因为它需要附加 QUERY PARAMS,这是 PARAMS 字典的一部分
-
@BeingSuman,我也在做同样的事情,但找不到 LISTING_URL。此外,该网站看起来受到 cloudflare 的保护,这可能表明您的请求不是从浏览器生成的。
标签: python-3.x http web-scraping python-requests