今天写个爬虫爬取简书网上的文章,简书网也是厉害,大部分都是ajax的,连‘阅读更多‘也是ajax的,我本来想用selenium的,结果连个点击的地方也没有,也是幸好每个详情页面下面有一些不是ajax的文章,才能爬到文章
spider.py
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from js.items import JsItem
class JsSpiderSpider(CrawlSpider):
name = 'js_spider'
allowed_domains = ['jianshu.com']
start_urls = ['http://www.jianshu.com/']
#这个follow要写true,不然众多只能爬到首页的一点文章,写true能爬到详情页面的推荐阅读
rules = (
Rule(LinkExtractor(allow=r'.+www.jianshu.com/p/.+'), callback='parse_item', follow=True),
)
def parse_item(self, response):
print(response.url)
div = response.xpath("//div[@class='article']")
title = div.xpath(".//h1[@class='title']/text()"