【问题标题】:Python Recursive Scraping with Scrapy使用 Scrapy 进行 Python 递归抓取
【发布时间】:2014-03-07 18:37:11
【问题描述】:

我正在尝试制作一个抓取工具,可以提取链接、标题、价格和 craigslist 上的帖子正文。我已经能够获得价格,但它会返回页面上每个列表的价格,而不仅仅是特定行的价格。我也无法让它转到下一页并继续抓取。

这是我正在使用的教程 - http://mherman.org/blog/2012/11/08/recursively-scraping-web-pages-with-scrapy/

我已经尝试过这个帖子的建议,但仍然无法成功 - Scrapy Python Craigslist Scraper

我要抓取的页面是 - http://medford.craigslist.org/cto/

在链接价格变量中,如果我在 span[@class="l2"] 之前删除 // 它将不返回任何价格,但如果我将它留在那里它包括页面上的每个价格。

对于规则,我尝试过使用类标签,但它似乎挂在第一页上。我在想我可能需要单独的蜘蛛类?

这是我的代码:

#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:      CD
#
# Created:     02/03/2014
# Copyright:   (c) CD 2014
# Licence:     <your licence>
#-------------------------------------------------------------------------------
from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from craigslist_sample.items import CraigslistSampleItem
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.http import Request
from scrapy.selector import *
import sys

class PageSpider(BaseSpider):
    name = "cto"
    allowed_domains = ["medford.craigslist.org"]
    start_urls = ["http://medford.craigslist.org/cto/"]

    rules = (Rule(SgmlLinkExtractor(allow=("index\d00\.html", ), restrict_xpaths=('//span[@class="button next"]' ,))
        , callback="parse", follow=True), )

    def parse(self, response):
        hxs = HtmlXPathSelector(response)
        titles = hxs.select('//span[@class="pl"] | //span[@class="l2"]')

        for title in titles:
            item = CraigslistSampleItem()
            item['title'] = title.select("a/text()").extract()
            item['link'] = title.select("a/@href").extract()
            item['price'] = title.select('//span[@class="l2"]//span[@class="price"]/text()').extract()

            url = 'http://medford.craigslist.org{}'.format(''.join(item['link']))
            yield Request(url=url, meta={'item': item}, callback=self.parse_item_page)


    def parse_item_page(self, response):
        hxs = HtmlXPathSelector(response)

        item = response.meta['item']
        item['description'] = hxs.select('//section[@id="postingbody"]/text()').extract()
        return item

【问题讨论】:

    标签: python recursion xpath web-scraping scrapy


    【解决方案1】:

    这个想法很简单:找到divclass="content" 中的所有段落。然后从每个段落中提取链接、文本链接和价格。请注意,select() 方法目前已弃用,请改用xpath()

    这是parse()方法的修改版本:

    def parse(self, response):
        hxs = HtmlXPathSelector(response)
        rows = hxs.select('//div[@class="content"]/p[@class="row"]')
    
        for row in rows:
            item = CraigslistSampleItem()
            link = row.xpath('.//span[@class="pl"]/a')
            item['title'] = link.xpath("text()").extract()
            item['link'] = link.xpath("@href").extract()
            item['price'] = row.xpath('.//span[@class="l2"]/span[@class="price"]/text()').extract()
    
            url = 'http://medford.craigslist.org{}'.format(''.join(item['link']))
            yield Request(url=url, meta={'item': item}, callback=self.parse_item_page)
    

    这是我得到的示例:

    {'description': [u"\n\t\tHave a nice, sturdy, compact car hauler/trailer.  May be used for other hauling like equipstment, ATV's and the like,   Very solid and in good shape.   Parice to sell at only $995.   Call Bill at 541 944 2929 top see or Roy at 541 9733421.   \n\t"],
     'link': [u'/cto/4354771900.html'],
     'price': [u'$995'],
     'title': [u'compact sturdy car trailer ']}
    

    希望对您有所帮助。

    【讨论】:

    • 帮助很大,谢谢。在看到别人的代码后,我有一种感觉,xpath 是要走的路,但是 row[2]/div[3] 结构令人生畏。这要简单得多。关于我的规则中阻止它进入下一页列表的任何想法?
    • @ISuckAtLife 欢迎您。至少你需要继承 CrawlSpider 而不是 BaseSpider
    • 我在发布之后尝试了它并且能够让它跟随其他页面,但现在我无法将它保留在 /cto 部分中。我将解析函数更改为 parse_page,在规则中对其进行了更改,然后将规则更改为仅允许 Medford.craigslist.org 在末尾添加 /cto 或 cto/ 什么都不返回。一定要爱婴儿步骤:-)
    • 这就是我现在得到的:rules = ( Rule( SgmlLinkExtractor(allow_domains=("medford.craigslist.org", )), callback='parse_page', follow=True ), )
    • 我已将其发布在一个单独的问题中,再次感谢您的帮助。 stackoverflow.com/questions/22264141/…
    猜你喜欢
    • 1970-01-01
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多