类型错误:'_sre.SRE_Match' 对象没有属性 '__getitem__' [英] TypeError: '_sre.SRE_Match' object has no attribute '__getitem__'

查看:48
本文介绍了类型错误:'_sre.SRE_Match' 对象没有属性 '__getitem__'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前收到此错误,不知道是什么意思.这是一个scrapy python项目,这是我看到的错误:

I'm currently getting this error and don't know what is means. Its a scrapy python project, this is the error I'm seeing:

  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__).我想你想要

The result of a re.match call is a SRE_Match object, which does not support the [] operator (a.k.a. __getitem__). I think you want

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

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

Unfortunately, parts.group(1) is not mutable, so you'll have to make another variable to hold the changes you want to make to it.

这篇关于类型错误:'_sre.SRE_Match' 对象没有属性 '__getitem__'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆