V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
data2world
V2EX  ›  Python

scrapy 爬虫 返回的 response 没有下一页这个属性。

  •  
  •   data2world · 2018-03-05 17:29:10 +08:00 · 4485 次点击
    这是一个创建于 2215 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如题。

    举个例子:这个网站无法返回下一页。 https://bbs.hupu.com/21593789.html

    scrapy shell "https://bbs.hupu.com/21593789.html"
    
    print response.body
    

    body中没有next page属性 。什么原因啊?怎么解决?

    11 条回复    2018-03-06 09:57:58 +08:00
    welkinzh
        1
    welkinzh  
       2018-03-05 18:09:17 +08:00
    网页源代码里确实没有这个属性,应该是 js 加载的
    locoz
        2
    locoz  
       2018-03-05 18:18:51 +08:00
    那个下一页按钮是 js 加载的,可以用这里的 pageCount 判断
    data2world
        3
    data2world  
    OP
       2018-03-05 18:44:24 +08:00
    @locoz maxpage 我倒是能获取到,总的 urls 我也能拼。就是 scrapy 的 yield 是不按顺序返回的,而我需要 1 页爬了,再爬第二页。部分代码是这样的:
    ```python
    max_page = int(response.xpath('//*[@id="bbstopic_set"]/@data-maxpage').extract()[0])

    if max_page > 1:
    for page_num in range(2, max_page + 1):
    next_page_url = 'https://bbs.hupu.com/{}-{}.html'.format(response.meta["short_url"], page_num)
    print "****", next_page_url, "****"
    yield scrapy.Request(next_page_url, callback=self.parse_content, headers=self.headers,
    cookies=self.cookies,
    meta={"item": item})
    else:
    yield item

    def parse_content(self, response):
    pass
    ```
    flyxl
        4
    flyxl  
       2018-03-05 19:07:30 +08:00 via Android
    为什么一定要按页爬呢?是增量爬取还是数据库记录要有序?如果是数据库记录的问题,入库的时候记录一下当前是第几页
    data2world
        5
    data2world  
    OP
       2018-03-05 19:20:46 +08:00
    @flyxl 因为我需要爬所有的分页页面爬完之后合并成一个 list,在赋给父 item。但是 yield 是异步的,所以不能以 for 循环的执行次数判断是否爬取完了。
    zhijiansha
        6
    zhijiansha  
       2018-03-05 19:30:17 +08:00
    你可以每一页抓取完了再 yield 下一页,这样就按顺序来了
    Zzde
        7
    Zzde  
       2018-03-05 23:28:01 +08:00
    随便点几页规则就出来了

    21593789-{page}
    locoz
        8
    locoz  
       2018-03-05 23:47:12 +08:00 via Android
    @data2world 你要顺序的就别一次性生成所有页数的 url 啊,每次爬完判断一下有没有下一页,有的话当前页数+1 不就 ok 了😶
    data2world
        9
    data2world  
    OP
       2018-03-06 08:58:02 +08:00
    @Zzde。。。。你都没看懂我的问题,
    data2world
        10
    data2world  
    OP
       2018-03-06 08:59:56 +08:00
    @locoz js 找不到下一页,而且+1 的话并不是顺序的,因为没有阻塞,他可能回爬取后面的之后,再爬取前面的。
    locoz
        11
    locoz  
       2018-03-06 09:57:58 +08:00
    @data2world 所以说你不要一次性生成所有页的 url 丢进去不就完事了 你没给它丢后面页面的任务它怎么会爬后面的。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1014 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 19:38 · PVG 03:38 · LAX 12:38 · JFK 15:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.