TypeError: takes exactly 1 argument (0 given) - Scrapy
我在和斯莱皮一起工作。我想为每个请求生成一个唯一的用户代理。我有以下几点:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class ContactSpider(Spider): name ="contact" def getAgent(self): f = open('useragentstrings.txt') agents = f.readlines() return random.choice(agents).strip() headers = { 'user-agent': getAgent(), 'content-type':"application/x-www-form-urlencoded", 'cache-control':"no-cache" } def parse(self, response): open_in_browser(response) |
getagent从以下表单的列表中生成代理:
1 | "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36" |
然而,当我运行这个程序时,我得到:
1 2 3 4 5 | File"..spiders\contact_spider.py, line 35, in <module> class ContactSpider(Spider): File"..spiders\contact_spider.py", line 54, in ContactSpider 'user-agent': getAgent(), TypeError: getAgent() takes exactly 1 argument (0 given) |
1 2 3 4 5 6 7 8 9 10 11 12 13 | from helpers import getAgent class ContactSpider(Spider): name ="contact" headers = { 'user-agent': getAgent(), 'content-type':"application/x-www-form-urlencoded", 'cache-control':"no-cache" } def parse(self, response): open_in_browser(response) |
另请参见:类和实例方法之间的区别。
或者,作为一种替代方法,有一个