![](/img/trans.png)
[英]Scrapy needs to crawl all the next links on website and move on to the next page
[英]make scrapy move to next page recursively
我正試圖用scrapy刮掉這個頁面 。 我可以成功地抓取頁面上的數據,但我希望能夠從其他頁面中抓取數據。 (接下來說的那些)。 繼承了我的代碼的相關部分:
def parse(self, response):
item = TimemagItem()
item['title']= response.xpath('//div[@class="text"]').extract()
links = response.xpath('//h3/a').extract()
crawledLinks=[]
linkPattern = re.compile("^(?:ftp|http|https):\/\/(?:[\w\.\-\+]+:{0,1}[\w\.\-\+]*@)?(?:[a-z0-9\-\.]+)(?::[0-9]+)?(?:\/|\/(?:[\w#!:\.\?\+=&%@!\-\/\(\)]+)|\?(?:[\w#!:\.\?\+=&%@!\-\/\(\)]+))?$")
for link in links:
if linkPattern.match(link) and not link in crawledLinks:
crawledLinks.append(link)
yield Request(link, self.parse)
yield item
我得到了正確的信息:來自鏈接頁面的標題,但它根本就不是“導航”。 我怎么告訴scrapy導航?
看看Scrapy Link Extractors文檔。 它們是告訴蜘蛛遵循頁面鏈接的正確方法。
看一下您要抓取的頁面,我相信您應該使用2個提取器規則。 以下是一個簡單蜘蛛的示例,其規則適合您的TIMES網頁需要:
from scrapy.contrib.spiders import CrawlSpider,Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
class TIMESpider(CrawlSpider):
name = "time_spider"
allowed_domains = ["time.com"]
start_urls = [
'http://search.time.com/results.html?N=45&Ns=p_date_range|1&Ntt=&Nf=p_date_range%7cBTWN+19500101+19500130'
]
rules = (
Rule (SgmlLinkExtractor(restrict_xpaths=('//div[@class="tout"]/h3/a',))
, callback='parse'),
Rule (SgmlLinkExtractor(restrict_xpaths=('//a[@title="Next"]',))
, follow= True),
)
def parse(self, response):
item = TimemagItem()
item['title']= response.xpath('.//title/text()').extract()
return item
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.