python - Scrapy写的爬虫只能抓取前面几页是什么问题?
天蓬老师
天蓬老师 2017-04-18 10:10:11
[Python讨论组]

打算爬贴吧,我是想获取每一页的帖子的链接,然后再根据帖子链接提取帖子里面的内容,提取某一页帖子的链接的代码已经写好,但是发现只提取了3页爬虫就结束了,这是什么问题?这是我的代码:

#coding:utf-8
import scrapy

class TiebaSpider(scrapy.Spider):
    name = "tiebapost"
    start_urls = [
        'http://tieba.baidu.com/f?kw=%E6%B8%A1%E8%BE%B9%E9%BA%BB%E5%8F%8B&ie=utf-8&pn=0'
    ]

    def parse(self, response):
        output = open('e:/scrapy_tutorial/link.txt', 'w+')
        count = 0
        for post in response.css('p.j_th_tit'):
            post_link = post.css('a.j_th_tit::attr(href)').extract()
            output.write('http://tieba.baidu.com' + post_link[0] + '\n')
            count += 1
            print u"提取到的链接:", post_link
        print u'总共', count, u'条链接'

        next_page = response.css('a.pagination-item::attr(href)').extract_first()
        if next_page is not None:
            yield scrapy.Request(next_page, callback=self.parse)
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(1)
PHP中文网

被 tieba.baidu.com 批量爬取时,会产生 403 或其它非200 的响应代码,这里页面就打不开了,也就没有了下一页,建议看看这个文档遇到的问题

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号