【问题标题】:scrapy crawler not working from home pagescrapy爬虫无法从主页工作
【发布时间】:2014-04-26 04:27:20
【问题描述】:

我在http://www.shop.ginakdesigns.com/main.sc 上写了一个潦草的涂鸦,试图收集物品

from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import Selector

from .. import items

class GinakSpider(CrawlSpider):
    name = "ginak"
    start_urls = [
   "http://www.shop.ginakdesigns.com/main.sc"
    ]
    rules = [Rule(SgmlLinkExtractor(allow=[r'category\.sc\?categoryId=\d+'])),
        Rule(SgmlLinkExtractor(allow=[r'product\.sc\?productId=\d+&categoryId=\d+']), callback='parse_item')]

def parse_item(self, response):
    sel = Selector(response)
    self.log(response.url)
    item = items.GinakItem()
    item['name'] = sel.xpath('//*[@id="wrapper2"]/div/div/div[1]/div/div/div[2]/div/div/div[1]/div[1]/h2/text()').extract()
    item['price'] = sel.xpath('//*[@id="listPrice"]/text()').extract()
    item['description'] = sel.xpath('//*[@id="wrapper2"]/div/div/div[1]/div/div/div[2]/div/div/div[1]/div[4]/div/p/text()').extract()
    item['category'] = sel.xpath('//*[@id="breadcrumbs"]/a[2]/text()').extract()

    return item

但是它不会超出主页进入任何链接。我已经尝试了各种方法并检查了 SgmlLinkExtractor 的正则表达式。这里有什么问题吗?

【问题讨论】:

    标签: python html web-scraping web-crawler scrapy


    【解决方案1】:

    问题是您尝试提取的链接中插入了jsessionid,例如:

    <a href="/category.sc;jsessionid=EA2CAA7A3949F4E462BBF466E03755B7.m1plqscsfapp05?categoryId=16">
    

    通过对任何字符使用.*?非贪婪匹配而不是寻找/?来修复它:

    rules = [Rule(SgmlLinkExtractor(allow=[r'category\.sc.*?categoryId=\d+']), callback='parse_item'),
             Rule(SgmlLinkExtractor(allow=[r'product\.sc.*?productId=\d+&categoryId=\d+']), callback='parse_item')]
    

    希望对您有所帮助。

    【讨论】:

    • 谢谢!效果很好。您查看了 html 源代码就知道了?
    猜你喜欢
    • 2023-03-05
    • 2019-06-24
    • 2022-06-15
    • 2017-11-07
    • 1970-01-01
    • 2014-10-04
    • 1970-01-01
    • 2013-06-18
    • 2021-12-26
    相关资源
    最近更新 更多