我目前收到此错误,不知道是什么意思。它是一个python项目,这是我看到的错误:
File "/bp_scraper/bp_scraper/httpmiddleware.py", line 22, in from_crawler return cls(crawler.settings) File "/bp_scraper/bp_scraper/httpmiddleware.py", line 12, in __init__ if parts[1]: TypeError: '_sre.SRE_Match' object has no attribute '__getitem__'
代码:
import re import random import base64 from scrapy import log class RandomProxy(object): def __init__(self, settings): self.proxy_list = settings.get('PROXY_LIST') f = open(self.proxy_list) self.proxies = {} for l in f.readlines(): parts = re.match('(\w+://)(\w+:\w+@)?(.+)', l) if parts[1]: parts[1] = parts[1][:-1] self.proxies[parts[0] + parts[2]] = parts[1] f.close() @classmethod def from_crawler(cls, crawler): return cls(crawler.settings)
在此先感谢你的帮助!
re.match调用的结果是一个SRE_Match对象,该对象不支持[]运算符(aka __getitem__)。我想你要
re.match
SRE_Match
aka __getitem__
if parts is not None: if parts.group(1): <blah>
不幸的是,parts.group(1)它不是可变的,因此你必须创建另一个变量来保存要对其进行的更改。
parts.group(1)