TypeError: object '_sre.SRE_Match' does not have attribute '__getitem__'

I am currently getting this error and don't know what it means. His pipeon scrapy project, this is the error I see:

  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__'

The code:

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)

Thanks in advance for your help!

+4
source share
2 answers

The result of the call re.matchis an object SRE_Matchthat the [](aka __getitem__) operator does not support . I think you want

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

Unfortunately, it parts.group(1)does not change, so you will have to make another variable to hold back the changes you want to make.

+7
source

You cannot access consistent results like:

        if parts[1]:
            parts[1] = parts[1][:-1]

Do it instead

        if parts:
            matched = parts.group(1)[:-1]

+3

Source: https://habr.com/ru/post/1531070/


All Articles