鱼C论坛

 找回密码
 立即注册
12
返回列表 发新帖
楼主: fan1993423

关于scrapy crawlspider

[复制链接]
 楼主| 发表于 2018-12-13 12:33:52 | 显示全部楼层
塔利班 发表于 2018-12-13 11:21
爬虫方面我感觉真不是大佬,我觉得楼上的和幽梦三影都可以,多线程爬取吧,
最近事情比较多,本人还得 ...

好吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-13 13:19:08 | 显示全部楼层
归类的话用导航页http://news.sina.com.cn/guide/。我的思路就是你爬一次导航页把类别归好,然后就是文章页的url匹配到细分类中的一个,把它塞过去就好了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-12-13 16:10:19 | 显示全部楼层
zyj321321312 发表于 2018-12-13 13:19
归类的话用导航页http://news.sina.com.cn/guide/。我的思路就是你爬一次导航页把类别归好,然后就是文章页 ...

归类我倒是实现了,就是如何将文章页放到小类里,我弄得很头疼,我用的是scrapy crawlspider来写的
  1. class XlSpider(CrawlSpider):
  2.     name = 'xl'
  3.     #allowed_domains = ['https://www.sina.com.cn/']
  4.     start_urls = ['https://www.sina.com.cn/']

  5.     rules = (Rule(LinkExtractor(allow=r'https://www.sina.com.cn/'), callback='parse_parent', follow=False),
  6.              Rule(LinkExtractor(restrict_xpaths='//div[@class="main-nav"]/div[@class!="nav-mod-1 nav-w nav-hasmore"]/ul/li[position()>1]'), follow=True),
  7.              Rule(LinkExtractor(allow='https:.+\.shtml'),follow=True,callback='parse_article'))
  8.     def parse_parent(self, response):
  9.         item = XinlangItem()
  10.         a=response.xpath('//div[@class="main-nav"]/div[@class!="nav-mod-1 nav-w nav-hasmore"]/ul')
  11.         for each in a:
  12.             li1title=each.xpath('./li[1]/a/b/text()').extract()[0]
  13.             li2title = each.xpath('./li[2]/a/text()').extract()[0]
  14.             li3title = each.xpath('./li[3]/a/text()').extract()[0]
  15.             li4title = each.xpath('./li[4]/a/text()').extract()[0]
  16.             pname='e:/小爬爬成果/新浪/'+li1title
  17.             li1url=each.xpath('./li[1]/a/@href').extract()[0]
  18.             li2url = each.xpath('./li[2]/a/@href').extract()[0]
  19.             li3url = each.xpath('./li[3]/a/@href').extract()[0]
  20.             li4url = each.xpath('./li[4]/a/@href').extract()[0]
  21.             if not os.path.exists(pname):
  22.                 os.makedirs(pname+'/'+li2title)
  23.                 os.makedirs(pname+'/'+li3title)
  24.                 os.makedirs(pname+'/'+li4title)
  25.             item['parent_title']=li1title
  26.             item['sub_title']=li2title+','+li3title+','+li4title
  27.             item['parent_url']=li1url
  28.             item['son_url']=li2url+','+li3url+','+li4url
  29.             yield item
  30.     def parse_article(self, response):
  31.         item = XinlangItem()
  32.         item['head']=response.xpath('//h1[@class="main-title"]/text()').extract()[0]
  33.         b=response.xpath('//div[@id="article"]/p/text()').extract()
  34.         content=''
  35.         for each in b:
  36.             content+=each
  37.         item['content']=content
  38.         yield item
复制代码

这是我写的,要不你也写一下吧,看了代码就一清二楚了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-12 08:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表