作业笔记08_weixin

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

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


代码部分:

用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被封了。又厚颜无耻的去请人家解除掉。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • scrapy学习笔记(有示例版) 我的博客 scrapy学习笔记1.使用scrapy1.1创建工程1.2创建爬虫模...
    陈思煜阅读 13,021评论 4 46
  • 1 前言 作为一名合格的数据分析师,其完整的技术知识体系必须贯穿数据获取、数据存储、数据提取、数据分析、数据挖掘、...
    whenif阅读 18,273评论 45 523
  • 我们曾想过要成为很多人却唯独忘了要成为自己,而她不问前世今生,只想做自己,唯一的自己。 一、 二月十二,风轻云淡,...
    生生云阅读 1,246评论 1 0
  • 从心底里害怕自己不值得被爱,所以我一直独来独往。 因为我一直独来独往,所以觉得自己不值得被爱。
    熊田野阅读 149评论 0 0
  • 那天和女儿一起读绘本《你好,世界》,读到一段:“孩子,是你让我第二次重新爱上了这个世界”,一瞬间我的眼泪就溢...
    Chelsea紫裳阅读 282评论 0 0

友情链接更多精彩内容