请教一个问题,怎么提高 python 爬虫的爬取效率
代码使用了 coroutine ,但仍然是单线程在跑,没有利用到多核的优势,如果不考虑对方的反爬而只考虑效率的提高的话 可以再加上多进程试试
写了个简单的协程爬虫爬取 b 站用户信息,代码如下:
import requestsimport reimport json
import datetimeimport asynciodef get_**(uid):
url_** = "htax/member/get**?mid=" #基本信息
uid = str(uid) return loop.run_in_executor(none, requests.get, url_**+uid)
async def user_**(num):
for uid in range(num, num+10):
** = await get_**(uid)
** = json.loads(**.text)["data"] try: # print(datetime.datetime.fromtimestamp(**['regtime']))
print("ok", uid)
print(**) except unicodeencodeerror as e:
print("unicodeencodeerror:", e) except typeerror:
print(**)
loop = asyncio.get_event_loop()try:
loop.run_until_complete(asyncio.wait([user_**(x) for x in range(1, 1000, 10)]))except exception as e:
print("error:", e)
爬取 1000 条需要 50 秒左右,而且带宽占用也只有 220kbps 左右的样子,有没有什么办法提高爬取的速度? b 站用户有 3800 万左右。 20210311