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
bb2018
V2EX  ›  Python

用 scrapy 采集百度贴吧,怎么按内容或标题关键字采集贴子?那个 xpath 写了总是采不到..?

  •  
  •   bb2018 · 2017-04-27 12:30:46 +08:00 · 1555 次点击
    这是一个创建于 2560 天前的主题,其中的信息可能已经有所发展或是发生改变。

    比如:我想采集贴吧,标题 或内容含有关键词:征婚 交友 美女 这几个关键词的全部贴子.

    下面代码可以采集全部贴子:

    item['title'] = response.xpath('//h1[@style="width: 470px"]/text()').extract()[0].strip() ####贴子标题 item['url'] = response.meta['text_url'] ####贴子地址 item['content'] = response.xpath('//*[starts-with(@id, "post_content_")]/text()').extract()[0].strip() ####贴子的内容 item['time'] = response.xpath('//div[@class="l_post j_l_post l_post_bright noborder "]').re("\d+-\d+-\d+ \d+:\d+") ####发贴时间 item['click'] = random.randint(0, 20) ###点击次数,给了一个随机值

    用下面的两个方法.先查一下内容再决定要不要

    ################ 方法 二

    okok = response.xpath('//[starts-with(@id, "post_content_")]/text()').extract()[0].strip() if '交友' or '征婚' or '美女' in okok:    item['content'] = response.xpath('//[starts-with(@id, "post_content_")]/text()').extract()[0].strip()    item['title'] = response.xpath('//h1[@style="width: 470px"]/text()').extract()[0].strip()    item['url'] = response.meta['text_url']    item['time'] = response.xpath('//div[@class="l_post j_l_post l_post_bright noborder "]').re("\d+-\d+-\d+ \d+:\d+")    item['click'] = random.randint(0, 20)    print item    yield item

    这两个总是不行.也用过 contains(str1, str2) 可能是用的不行.总也不成功.

    不知道有什么办法.可以通过一组关键词采集百度贴子.

    谢谢.

    byfar
        1
    byfar  
       2017-04-27 13:10:41 +08:00
    XPath 小技巧:如果是 chrome 浏览器的话,开发都工具的 Elements 下 ctrl+f 有 find by string, selector, or XPath 的功能。

    取不到要么是你取的元素是 ajax 异步请求的,可以模拟请求。

    要么你的 xpath 表达式有问题,可以用上述方法检验。
    bb2018
        2
    bb2018  
    OP
       2017-04-27 15:02:54 +08:00
    @byfar 不是取不到.是能取到全部贴子信息.但是现在是无法挑选内容有:交友  征婚 美女等特定关键词的方法.
    不知道是在采集的时候过虑还是在入库的时候过虑.应该怎么过虑出来要的信息?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1098 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 78ms · UTC 23:58 · PVG 07:58 · LAX 16:58 · JFK 19:58
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.