这是我的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个链接/ URL ...
<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 并停止。 未报告任何错误。好像这个递归部分不太有用...... !那么如何修改此代码以转到这些3000个网址中的每一个,然后获取一些特定字段。
我在其他一些类似的问题中看到,人们已经使用了&#34;规则&#34;和Scrapy&#34; LinkExtractor&#34;宾语?我不确定我是否需要其中任何一种,因为我的要求很简单。
非常感谢任何帮助。感谢
答案 0 :(得分: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"]
,这可能是另一个原因。
答案 1 :(得分: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)
最后两行做了递归魔术......