作者刚入门Python爬虫,如代码有问题,请大佬评论区指出!!
代码实现:
1,通过multiprocessing中的进程池实现多进程爬虫。
2,通过requests发起get请求刷浏览量。
代码:
from multiprocessing import Lock, pool
import requests# 爬取次数
count = 0# 爬虫函数
def spider(url, count):response = requests.get(url)print(f'status code : {response.status_code}, times: {count}')return response.status_codeif __name__ == '__main__':pool = pool.Pool(processes=100) # 进程池,参数是进程数量for i in range(10000): # 爬的次数# 非阻塞式对每一个进程调用爬虫函数result = pool.apply_async(spider, ('http://www.scazzx.cn/News/NewsOne.asp?News_ID=2271', count))count += 1print('Started')# 阻塞主进程pool.close()# 等待子进程pool.join()print('Ended')
效果: