TypeError:不能在Python中的byteslike对象上使用字符串模式

2024-05-08 11:43:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Scrapy制作一个电子邮件刮板,我不断收到以下错误: TypeError:不能在类似字节的对象上使用字符串模式

下面是我正在使用的Python代码:

import re
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor

class EmailSpider(CrawlSpider):
    name = 'EmailScraper'
    emailHistory = {}
    custom_settings = {
        'ROBOTSTXT_OBEY': False
        #  ,'DEPTH_LIMIT' : 6
    }

emailRegex = re.compile((r"([a-zA-Z0-9_{|}~-]+(?:\.[a-zA-Z0-9_"
                         r"{|}~-]+)*(@)(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9]){2,}?(\."
                         r"))+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)"))

def __init__(self, url=None, *args, **kwargs):
    super(EmailSpider, self).__init__(*args, **kwargs)
    self.start_urls = [url]
    self.allowed_domains = [url.replace(
        "http://", "").replace("www.", "").replace("/", "")]
rules = (Rule(LinkExtractor(), callback="parse_item", follow=True),)

def parse_item(self, response):
    emails = re.findall(EmailSpider.emailRegex, response._body)
    for email in emails:
        if email[0] in EmailSpider.emailHistory:
            continue
        else:
            EmailSpider.emailHistory[email[0]] = True
            yield {
                'site': response.url,
                'email': email[0]
            }

我已经看到了很多答案,但我对python非常陌生,所以我不确定如何在代码中实现给定的代码。你知道吗

所以如果你不介意的话,也可以告诉我要把代码放进去。你知道吗

谢谢,裘德·威尔逊


Tags: 代码fromimportselfreurlemailresponse
1条回答
网友
1楼 · 发布于 2024-05-08 11:43:20

response._body不是str(字符串对象),因此不能对其使用re(regex)。如果您查找它的对象类型,您会发现它是一个bytes(bytes对象)。你知道吗

>>> type(response._body)
<class 'bytes'>

通过把它解码成UTF-8之类的东西,问题就应该解决了。你知道吗

>>> type(response._body.decode('utf-8'))
<class 'str'>

最后的re是这样的:

emails = re.findall(EmailSpider.emailRegex, response._body.decode('utf-8'))

相关问题 更多 >