python - 类型错误 : '_sre.SRE_Match' object has no attribute '__getitem__'

标签 python scrapy

我目前遇到此错误,但不知道是什么意思。这是一个 scrapy 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 对象,它不支持 [] 运算符(又名 __getitem__).我想你想要

if parts is not None:
    if parts.group(1):
        <blah>

不幸的是,parts.group(1) 不是可变的,因此您必须创建另一个变量来保存您想要对其进行的更改。

关于python - 类型错误 : '_sre.SRE_Match' object has no attribute '__getitem__' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22313702/

相关文章:

python - 从列表中删除字典

python - 使用多列进行 Pandas 滚动应用

python - 在Scrapy中,如何设置每个url的时间限制?

python - 谷歌搜索

python - scrapy:禁用表单请求的过滤是否有意义?

python - 为什么 Pycharm 不能运行相对导入的代码?

python mysql连接问题

python - Scrapy安全提取元素的方法

python - Scrapy-Splash:无法使用 scrapinghub/splash:latest 作为基础镜像运行 docker 容器

python - Pandas 数据帧 : how can i compare values in two columns of a row are equal to the ones in the same columns of a subsequent row?