我正在使用scrapy從craiglist收集一些電子郵件,當我運行它時,它會返回空白行.csv文件。我能夠提取標題,標記和鏈接。只有電子郵件是問題。下面是代碼:Scrapy不收集數據
所有的# -*- coding: utf-8 -*-
import re
import scrapy
from scrapy.http import Request
# item class included here
class DmozItem(scrapy.Item):
# define the fields for your item here like:
link = scrapy.Field()
attr = scrapy.Field()
title = scrapy.Field()
tag = scrapy.Field()
class DmozSpider(scrapy.Spider):
name = "dmoz"
allowed_domains = ["craigslist.org"]
start_urls = [
"http://raleigh.craigslist.org/bab/5038434567.html"
]
BASE_URL = 'http://raleigh.craigslist.org/'
def parse(self, response):
links = response.xpath('//a[@class="hdrlnk"]/@href').extract()
for link in links:
absolute_url = self.BASE_URL + link
yield scrapy.Request(absolute_url, callback=self.parse_attr)
def parse_attr(self, response):
match = re.search(r"(\w+)\.html", response.url)
if match:
item_id = match.group(1)
url = self.BASE_URL + "reply/nos/vgm/" + item_id
item = DmozItem()
item["link"] = response.url
item["title"] = "".join(response.xpath("//span[@class='postingtitletext']//text()").extract())
item["tag"] = "".join(response.xpath("//p[@class='attrgroup']/span/b/text()").extract()[0])
return scrapy.Request(url, meta={'item': item}, callback=self.parse_contact)
def parse_contact(self, response):
item = response.meta['item']
item["attr"] = "".join(response.xpath("//div[@class='anonemail']//text()").extract())
return item
以'start_urls'開頭的默認回調函數是'parse()',而不是'parse_contact()'。另外,'start_urls'中定義的URL中沒有郵件,所以你的xpath不匹配任何東西。您是否閱讀過[Scrapy教程](http://doc.scrapy.org/en/latest/intro/tutorial.html)?這些東西在那裏解釋。 – bosnjak
迄今爲止,這段代碼爲我工作,但最近兩天似乎在craiglist上修改了一些東西。你能否添加工作代碼?在此先感謝 –
@ArkanKalu您需要提供您的蜘蛛的完整代碼。 – alecxe