有一说一发这篇我有点惭愧,因为别人都讲过了,不过既然自己做了一遍,就还是记下来。
感谢这位老铁
首先问题是:自用的类里加了fake-useragent,导入时报错timeout如下,据说是个别服务器通讯更新假头时连接超时,所以可以手工把下到的假头存在本地临时文件里。
- 从这里手工下载假头;
- 然后使用如下脚本得到缓存文件存放的目录
import tempfile
print(tempfile.gettempdir())
# 得到类似C:\Users\bob\AppData\Local\Temp
-
然后将1.中得到的json格式的数据存放在名为fake_useragent_0.1.11.json的文件中,放在上面的缓存地址(如果版本更新,可能需要对应的修改1.中的地址以及这里的文件名)
-
正常运行
-
可以执行以上过程的脚本如下
from os import listdir
from os.path import join
from requests import get
from tempfile import gettempdir
if 'fake_useragent_0.1.11.json' not in listdir(gettempdir()):
fku = get('https://fake-useragent.herokuapp.com/browsers/0.1.11')
with open(join(gettempdir(),'fake_useragent_0.1.11.json'),"w") as wrt:
wrt.write(fku.text)