美文网首页
作业笔记08_weixin

作业笔记08_weixin

作者: ChZ_CC | 来源:发表于2017-02-07 14:08 被阅读69次

作业内容:选择你感兴趣的方向,编写一个小型的爬虫程序。

老喜欢和菜头了,来爬一下他的公众号文章吧。从传送门进去,爬取所有的文章列表、发表时间和链接。


代码部分:

用scrapy来做吧,省事一点。

新建项目:scrapy startproject bitsea

  1. item.py
import scrapy

class BitseaItem(scrapy.Item):
    title = scrapy.Field()
    timestamp = scrapy.Field()
    articleUrl = scrapy.Field()
  1. parse程序:article.py

    有两个问题:

    1. 这个传送门的网址直接爬取的话,会403Forbidden。开始不知道为什么,折腾了好多遍,才看到403的提示。所以加上了header和cookie,之后可以读取到网页数据了。

    2. 页数比较多,爬到一半出现503错误。好像是因为操作太频繁。于是加了个时间间隔,终于能完整的爬下来了。

    from scrapy.spider import Spider
    from scrapy.selector import Selector
    from scrapy.http import Request
    import time
    
    from bitsea.items import BitseaItem
    
    
    class bitsea(Spider):
        name = "bitsea"
        #allowed_domains = ['chuansong.me/'] 
    
        headers = {
            "Accept":'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
            "Accept-Encoding":'gzip, deflate, sdch',
            "Accept-Language":'zh-CN,zh;q=0.8',
            "Cache-Control":'max-age=0',
            "Connection":'keep-alive',
            "User-Agent":'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.87 Safari/537.36'
        }
    
        cookies = {
            'BDTUJIAID' : '5ac91e7d81da8df68542b83635f53043',
            'Hm_lvt_9d416cb1bdca85038c8810fc14eeee57' : '1481620340,1482893943,1482975103,1482975314',
            'Hm_lpvt_9d416cb1bdca85038c8810fc14eeee57' : '1482977629',
            '__utmt' : '1',
            '__utma' : '240057436.12323463.1473405823.1482975103.1482975314.8',
            '__utmb' : '240057436.3.10.1482975314',
            '__utmc' : '240057436',
            '__utmz' : '240057436.1482975314.8.2.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided)'
        } 
    
        def start_requests(self):
            return [Request("http://chuansong.me/account/bitsea", cookies = self.cookies, headers = self.headers)]
    
        def parse(self, response):
            for art in response.xpath('//div[@class="pagedlist_item"]'):
                yield {
                    'title': art.xpath('.//a[@class="question_link"]/text()').extract_first().strip(),
                    'timestamp': art.xpath('.//span[@class="timestamp"]/text()').extract(),
                    'articleUrl': art.xpath('.//a[@class="question_link"]/@href').extract()
                }
    
                time.sleep(0.5)
    
            next_page = response.xpath('//div[@class="w4_5"]/a[@style="float: right"]/@href').extract()
            if next_page is not None:
                next_page = 'http://chuansong.me'+next_page[0]
                yield Request(next_page, callback=self.parse, cookies = self.cookies, headers = self.headers)
    
    

结果输出:

一共六七百篇文章,截一部分图出来吧。


作死小记:

本来打算把文章内容爬下来修改成markdown格式,结果就个标题就折腾了一天。爬虫伤身,注意安全。

后来把time.sleep()改小了一点,被人家的反爬虫机制检测到了应该是,我的IP被封了。又厚颜无耻的去请人家解除掉。

相关文章

网友评论

      本文标题:作业笔记08_weixin

      本文链接:https://www.haomeiwen.com/subject/ijapittx.html