python gevent monkey模块报错请教下!
再学习gevent 模块是报错:from gevent import monkey
#从gevent库里导入monkey模块。
monkey.patch_all()
#monkey.patch_all()能把程序变成协作式运行,就是可以帮助程序实现异步。
import gevent,time,requests
#导入gevent、time、requests。
start = time.time()
#记录程序开始时间。
url_list = ['https://www.baidu.com/',
'https://www.sina.com.cn/',
'http://www.sohu.com/',
'https://www.qq.com/',
'https://www.163.com/',
'http://www.iqiyi.com/',
'https://www.tmall.com/',
'http://www.ifeng.com/']
#把8个网站封装成列表。
def crawler( ):
#定义一个crawler()函数。
r = requests.get(url)
#用requests.get()函数爬取网站。
print(url,time.time()-start,r.status_code)
#打印网址、请求运行时间、状态码。
tasks_list = [ ]
#创建空的任务列表。
for url in url_list:
#遍历url_list。
task = gevent.spawn(crawler,url)
#用gevent.spawn()函数创建任务。
tasks_list.append(task)
#往任务列表添加任务。
gevent.joinall(tasks_list)
#执行任务列表里的所有任务,就是让爬虫开始爬取网站。
end = time.time()
#记录程序结束时间。
print(end-start)
#打印程序最终所需时间。
运行后报错:
Traceback (most recent call last):
File "src\gevent\greenlet.py", line 766, in gevent._greenlet.Greenlet.run
TypeError: crawler() takes 0 positional arguments but 1 was given
2019-08-11T06:40:00Z <Greenlet at 0x1cc4777ca48: crawler('https://www.baidu.com/')> failed with TypeError
Traceback (most recent call last):
File "src\gevent\greenlet.py", line 766, in gevent._greenlet.Greenlet.run
TypeError: crawler() takes 0 positional arguments but 1 was given
2019-08-11T06:40:00Z <Greenlet at 0x1cc4777cb48: crawler('https://www.sina.com.cn/')> failed with TypeError
Traceback (most recent call last):
File "src\gevent\greenlet.py", line 766, in gevent._greenlet.Greenlet.run
TypeError: crawler() takes 0 positional arguments but 1 was given
2019-08-11T06:40:00Z <Greenlet at 0x1cc4777cc48: crawler('http://www.sohu.com/')> failed with TypeError
Traceback (most recent call last):
File "src\gevent\greenlet.py", line 766, in gevent._greenlet.Greenlet.run
TypeError: crawler() takes 0 positional arguments but 1 was given
2019-08-11T06:40:00Z <Greenlet at 0x1cc4777cd48: crawler('https://www.qq.com/')> failed with TypeError
Traceback (most recent call last):
File "src\gevent\greenlet.py", line 766, in gevent._greenlet.Greenlet.run
TypeError: crawler() takes 0 positional arguments but 1 was given
2019-08-11T06:40:00Z <Greenlet at 0x1cc4777ce48: crawler('https://www.163.com/')> failed with TypeError
Traceback (most recent call last):
File "src\gevent\greenlet.py", line 766, in gevent._greenlet.Greenlet.run
TypeError: crawler() takes 0 positional arguments but 1 was given
2019-08-11T06:40:00Z <Greenlet at 0x1cc47839048: crawler('http://www.iqiyi.com/')> failed with TypeError
Traceback (most recent call last):
File "src\gevent\greenlet.py", line 766, in gevent._greenlet.Greenlet.run
TypeError: crawler() takes 0 positional arguments but 1 was given
2019-08-11T06:40:00Z <Greenlet at 0x1cc47839148: crawler('https://www.tmall.com/')> failed with TypeError
Traceback (most recent call last):
File "src\gevent\greenlet.py", line 766, in gevent._greenlet.Greenlet.run
TypeError: crawler() takes 0 positional arguments but 1 was given
2019-08-11T06:40:00Z <Greenlet at 0x1cc47839248: crawler('http://www.ifeng.com/')> failed with TypeError
0.015010833740234375
已经安装了最新的greenlet库。求解…… python版本是3.6.3 gevent是1.4的版本了! 第22行 改为 def crawler( url): TypeError: crawler() takes 0 positional arguments but 1 was given
crawler函数应该有1个参数 task = gevent.spawn(crawler,url)这样写的话,会把 url 传入 crawler 做为它的一个参数,但你的 crawler 定义时是不需要参数的
页:
[1]