首页 文章

Scrapy只爬1页

提问于
浏览
1

这是我的Scrapy代码......

import scrapy

class NewsSpider(scrapy.Spider):
name = "news"
start_urls = ['http://www.StartURL.com/scrapy/all-news-listing']
allowed_domains = ["www.xxxxx.com"]

def parse(self, response):
    for news in response.xpath('head'):
        yield {
    'pagetype': news.xpath('//meta[@name="pdknpagetype"]/@content').extract(),
    'pagetitle': news.xpath('//meta[@name="pdknpagetitle"]/@content').extract(),
    'pageurl': news.xpath('//meta[@name="pdknpageurl"]/@content').extract(),
    'pagedate': news.xpath('//meta[@name="pdknpagedate"]/@content').extract(),
    'pagedescription': news.xpath('//meta[@name="pdknpagedescription"]/@content').extract(),
    'bodytext': [' '.join(item.split()) for item in (response.xpath('//div[@class="module__contentp"]/*/node()/text()').extract())],
        }

    next_page = response.css('p a::attr(href)').extract_first()
    if next_page is not None:
        next_page = response.urljoin(next_page)
        yield scrapy.Request(next_page, callback=self.parse)

我的 start_urls 页面如下所示 . 这是一个非常简单的页面,列出了我想抓取的所有3000个链接/网址...

<html>
<head>
<div>
<p><a href="http://www.xxxxx.com/asdas-sdf/kkm">Page 1</a></p>
<p><a href="http://www.xxxxx.com/vdfvd-asda/vdfvf/dfvd">Page 2</a></p>
<p><a href="http://www.xxxxx.com/oiijo/uoiu/xwswd">Page 3</a></p>
<p><a href="http://www.xxxxx.com/jnkjn-yutyy/hjj-sdf/plm">Page 4</a></p>
<p><a href="http://www.xxxxx.com/unhb-oiiuio/hbhb/jhjh/qwer">Page 5</a></p>
<p><a href="http://www.xxxxx.com/eres/popo-hbhh/oko-sdf/ynyt">Page 6</a></p>
<p><a href="http://www.xxxxx.com/yhbb-ytyu/oioi/rtgb/ttyht">Page 7</a></p>
..........
<p><a href="http://www.xxxxx.com/iojoij/uhuh/page3000">Page 3000</a></p>
</div>
</head>
</html>

当我将Scrapy发送到此页面时,它只是抓取第一个链接,即http://www.xxxxx.com/page1并停止 . No errors reported. Seems like this recursion part is not quite working... !那么如何修改此代码以转到这些3000个网址中的每一个,然后获取一些特定字段 .

我在其他一些类似的问题中看到,人们使用过“Rules”和Scrapy的“LinkExtractor”对象?我不确定我是否需要其中任何一种,因为我的要求非常简单 .

非常感谢任何帮助 . 谢谢

2 回答

  • 0

    每次请求像 http://www.xxxxx.com/page1 这样的页面时,如果页面的页面栏没有改变,您可能会在 next_page = response.css('p a::attr(href)').extract_first() 上获得相同的结果 . 有更好的方法:

    start_urls = ['http://www.xxxxx.com/page{}'.format(i) for i in range(the last page number)]
    

    这样,您就不需要使用回调 .

    此代码中不需要 allowed_domains = ["www.xxxxx.com"] ,这可能是另一个原因 .

  • 0

    我怀疑,这确实是递归逻辑中的一个缺陷 .

    以下代码解决了我的问题....

    from scrapy.selector import Selector
    from scrapy.spider import BaseSpider
    from scrapy.http import Request
    
    class MySpider(BaseSpider):
        name = "pdknnews"
        start_urls = ['http://www.example.com/scrapy/all-news-listing/']
        allowed_domains = ["example.com"]
    
        def parse(self, response):
            hxs = Selector(response)
            for news in response.xpath('head'):
         yield {
            'pagetype': news.xpath('.//meta[@name="pdknpagetype"]/@content').extract(),
            'pagetitle': news.xpath('.//meta[@name="pdknpagetitle"]/@content').extract(),
            'pageurl': news.xpath('.//meta[@name="pdknpageurl"]/@content').extract(),
            'pagedate': news.xpath('.//meta[@name="pdknpagedate"]/@content').extract(),
            'pagedescription': news.xpath('.//meta[@name="pdknpagedescription"]/@content').extract(),
            'bodytext': [' '.join(item.split()) for item in (response.xpath('.//div[@class="module__content"]/*/node()/text()').extract())],
                }
        for url in hxs.xpath('//ul[@class="scrapy"]/li/a/@href').extract():
                yield Request(url, callback=self.parse)
    

    最后两行做了递归魔法......

相关问题