我正在使用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非常陌生,所以我不确定如何在代码中实现给定的代码。你知道吗
所以如果你不介意的话,也可以告诉我要把代码放进去。你知道吗
谢谢,裘德·威尔逊
response._body
不是str
(字符串对象),因此不能对其使用re
(regex)。如果您查找它的对象类型,您会发现它是一个bytes
(bytes对象)。你知道吗通过把它解码成UTF-8之类的东西,问题就应该解决了。你知道吗
最后的
re
是这样的:相关问题 更多 >
编程相关推荐