Python 如何使用Scrapy 0.24抓取站点并仅解析与正则表达式匹配的页面

Python 如何使用Scrapy 0.24抓取站点并仅解析与正则表达式匹配的页面,python,regex,scrapy,Python,Regex,Scrapy,我在Windows64位机器上使用Python2.7.9上的Scrapy0.24。我试图告诉scrapy从一个特定的URL开始http://www.allen-heath.com/products/并且仅从url包含字符串的页面收集数据 不幸的是,当我这样做的时候,根本没有数据被刮取。我做错了什么?下面是我的代码。如果有更多的信息我可以提供,以帮助回答,请询问,我会作出编辑 这是我的爬虫日志的粘贴库: 多谢各位 import scrapy import urlparse from allenh

我在Windows64位机器上使用Python2.7.9上的Scrapy0.24。我试图告诉scrapy从一个特定的URL开始
http://www.allen-heath.com/products/
并且仅从url包含字符串的页面收集数据

不幸的是,当我这样做的时候,根本没有数据被刮取。我做错了什么?下面是我的代码。如果有更多的信息我可以提供,以帮助回答,请询问,我会作出编辑

这是我的爬虫日志的粘贴库:

多谢各位

import scrapy
import urlparse

from allenheath.items import ProductItem
from scrapy.selector import Selector
from scrapy.http import HtmlResponse
from scrapy.contrib.spiders import Rule
from scrapy.contrib.linkextractors import LinkExtractor

class productsSpider(scrapy.Spider):
    name = "products"
    allowed_domains = ["http://www.allen-heath.com/"]
    start_urls = [
        "http://www.allen-heath.com/products/"
    ]
    rules = [Rule(LinkExtractor(allow=['ahproducts']), 'parse')]

    def parse(self, response):
        for sel in response.xpath('/html'):
            item = ProductItem()
            item['model'] = sel.css('#prodsingleouter > div > div > h2::text').extract()
            item['itemcode'] = sel.css('#prodsingleouter > div > div > h2::text').extract()
            item['shortdesc'] = sel.css('#prodsingleouter > div > div > h3::text').extract()
            item['desc'] = sel.css('#tab1 #productcontent').extract()
            item['series'] = sel.css('#pagestrip > div > div > a:nth-child(3)::text').extract()
            item['imageorig'] = sel.css('#prodsingleouter > div > div > h2::text').extract()
            item['image_urls'] = sel.css('#tab1 #productcontent .col-sm-9 img').xpath('./@src').extract()
            item['image_urls'] = [urlparse.urljoin(response.url, url) for url in item['image_urls']]
            yield item
根据eLRuLL的一些建议,这里是我更新的spider文件。我已经修改了start_url以包含一个页面,该页面的url中包含“ahproducts”链接。我的原始代码在起始页上没有任何匹配的URL

products.py

import scrapy
import urlparse

from allenheath.items import ProductItem
from scrapy.selector import Selector
from scrapy.http import HtmlResponse
from scrapy.contrib.spiders import Rule
from scrapy.contrib.linkextractors import LinkExtractor

class productsSpider(scrapy.contrib.spiders.CrawlSpider):
    name = "products"
    allowed_domains = ["http://www.allen-heath.com/"]
    start_urls = [
        "http://www.allen-heath.com/key-series/ilive-series/ilive-remote-controllers/"
    ]
    rules = (
            Rule(
                LinkExtractor(allow='.*ahproducts.*'),
                callback='parse_item'
                ),
            )

    def parse_item(self, response):
        for sel in response.xpath('/html'):
            item = ProductItem()
            item['model'] = sel.css('#prodsingleouter > div > div > h2::text').extract()
            item['itemcode'] = sel.css('#prodsingleouter > div > div > h2::text').extract()
            item['shortdesc'] = sel.css('#prodsingleouter > div > div > h3::text').extract()
            item['desc'] = sel.css('#tab1 #productcontent').extract()
            item['series'] = sel.css('#pagestrip > div > div > a:nth-child(3)::text').extract()
            item['imageorig'] = sel.css('#prodsingleouter > div > div > h2::text').extract()
            item['image_urls'] = sel.css('#tab1 #productcontent .col-sm-9 img').xpath('./@src').extract()
            item['image_urls'] = [urlparse.urljoin(response.url, url) for url in item['image_urls']]
            yield item

首先,要使用规则,您需要使用
scrapy.contrib.Spider.CrawlSpider
而不是
scrapy.Spider

然后,将方法的名称更改为
parse_item
not
parse
,并更新规则,如:

 rules = (
        Rule(
            LinkExtractor(allow='.*ahproducts.*'),
            callback='parse_item'
        ),
    )
始终调用
parse
方法作为
start\u URL
请求的响应

最后,仅将
允许的\u域更改为
允许的\u域=[“allen heath.com”]

p.D.要使用规则对站点的不同级别进行爬网,您需要指定要遵循的链接以及要解析的链接,如下所示:

rules = (
    Rule(
        LinkExtractor(
            allow=('some link to follow')
        ),
        follow=True,
    ),
    Rule(
        LinkExtractor(
            allow=('some link to parse')
        ),
        callback='parse_method',
    ),
)

URL与
ahproducts
匹配的页面链接在以
http://www.allen-heath.com/series/
/key-series/
。只能从
http://www.allen-heath.com/products/
。我有一个错误的印象,认为scrapy会关注
http://www.allen-heath.com/products/
直到找到匹配的页面。因此,如果我理解正确,使用规则,scrapy将只匹配并收集与我的规则匹配的链接中的数据,这些链接存在于我的
start\u url
上。在这种情况下,我想我需要首先生成许多
start\u url
s?根据您的评论,我已经将
class-productsSpider(scrapy.Spider)
更改为
class-productsSpider(scrapy.contrib.Spider.CrawlSpider):
并且我正在使用一个start\u url,它与
ahproducts
有链接。但是它仍然没有提取任何数据。另外,请检查是否需要将方法名称从
parse
更改为
parse\u item
,以检查是否正常工作。感谢您的回复。这对我来说是可行的,它获取了我期望的所有数据。只有几个问题需要澄清。在你的帖子里你说“P.D.”,那代表什么?其次,对于
允许的\u域
您删除了
http://www.
。我认为这样做的原因是考虑使用https://
的链接?最后,在第一个代码块中,回调是
parse_item
,但在第二个代码块中,您使用了
parse_method
。我用的是后者,效果很好。它们可以互换吗?@jimmykup,关于
parse.*
方法,您可以随意调用它,但是
parse
方法总是在start\u请求后调用(该方法调用start\u URL)。PD=postdata对此表示抱歉。