Pytrends持续返回429错误,使用代理后仅返回0的问题求助
环境与问题现象
- 系统:Windows 11
- Python版本:3.11.2
- 问题:正常使用Pytrends超过一年,约两周前(电脑崩溃前)开始,所有请求均返回错误:
The request failed: Google returned a response with code 429,更换新电脑后问题仍存在。
已尝试的解决方案
- 自定义请求头:从Google Trends站点抓取GET请求,通过工具生成Python代码后将请求头传入
request_args,无效果; - 版本回退:将certifi、urllib3、requests、pytrends回退至历史版本,无效果;
- 代理集成:购买美国地区代理并集成后,请求仅返回响应“0”。
相关代码
from pytrends.request import TrendReq import urllib.request import random username = 'USER' password = 'PASS' state = 'us_texas' entry = ('http://customer-%s-st-%s:%s@pr.oxylabs.io:7777' % (username, state, password)) query = urllib.request.ProxyHandler({ 'http': entry, 'https': entry, }) execute = urllib.request.build_opener(query) proxy_ip = execute.open('https://ipinfo.io').read() proxy_ip=str(proxy_ip) proxy_ip=proxy_ip.split("ip",1)[1] proxy_ip=proxy_ip.split(",",1)[0] proxy_ip=proxy_ip.replace("\"","",) proxy_ip=proxy_ip.replace(":","",) print(proxy_ip) proxy='https://'+proxy_ip proxy={1: proxy} print(proxy) pytrends = TrendReq(hl='en-US', tz=360, timeout=(10,25), retries=2, backoff_factor=0.1,proxies=proxy,requests_args={'verify':False})
当前已安装的pip包列表
aiohttp 3.8.4、aiosignal 1.3.1、appdirs 1.4.4、async-timeout 4.0.2、attrs 22.2.0、beautifulsoup4 4.12.0、bs4 0.0.1、certifi 2022.12.7、cffi 1.15.1、charset-normalizer 2.1.1、click 8.1.3、colorama 0.4.6、contourpy 1.0.7、cryptography 39.0.2、cssselect 1.2.0、cycler 0.11.0、DateTime 5.1、fake-useragent 1.1.3、feedparser 6.0.10、finviz 1.4.4、finvizfinance 0.14.5、fonttools 4.39.2、frozendict 2.3.6、frozenlist 1.3.3、get-all-tickers 1.8、html5lib 1.1、idna 3.4、importlib-metadata 6.1.0、joblib 1.2.0、kiwisolver 1.4.4、lxml 4.9.2、matplotlib 3.7.1、multidict 6.0.4、multitasking 0.0.11、nltk 3.8.1、numpy 1.24.2、packaging 23.0、pandas 1.5.3、parse 1.19.0、pause 0.3、Pillow 9.4.0、pip 23.0.1、pycparser 2.21、pyee 8.2.2、pyparsing 3.0.9、pyppeteer 1.0.2、pyquery 2.0.0、python-dateutil 2.8.2、pytickersymbols 1.13.0、pytrends 4.7.3、pytz 2022.7.1、PyYAML 6.0、regex 2022.10.31、requests 2.28.1、requests-futures 1.0.0、requests-html 0.10.0、scikit-learn 1.2.2、scipy 1.10.1、setuptools 65.5.0、sgmllib3k 1.0.0、six 1.16.0、sklearn 0.0.post1、soupsieve 2.4、tenacity 8.2.2、textblob 0.17.1、threadpoolctl 3.1.0、tqdm 4.65.0、urllib3 1.26.11、user-agent 0.1.10、w3lib 2.1.1、webencodings 0.5.1、websockets 10.4、wheel 0.38.4、yahoo-earnings-calendar 0.6.0、yahooquery 2.2.15、yarl 1.8.2、yfinance 0.2.13、zipp 3.15.0
内容的提问来源于stack exchange,提问作者Jon Harris

