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

scrapy,我现在把文章列表的文章 url 和 title 爬到了,但是怎么爬到文章内容呢?

  •  
  •   Ixizi · 2016-04-21 17:42:05 +08:00 · 3915 次点击
    这是一个创建于 2943 天前的主题,其中的信息可能已经有所发展或是发生改变。

    URL 已经有了,但是 scrapy 里面该怎么操作爬文章内容啊。

    class BlogSpider(CrawlSpider):
        name = 'bee'
        allowed_domains = ['xxx.com']
        start_urls = ['https://xxxxxx.com/blogs?']
        rules = (
            Rule(LinkExtractor(allow="page=[0-9]{1,20}"),follow=True,callback='parse_item'),
        )
        def parse_item(self,response):
            item = ScrapyBlogItem()
            title = response.xpath('//h2[@class="title"]/a/text()').extract()
            url = response.xpath('//h2[@class="title"]/a/@href').extract()
            item_dict = dict(zip(url,title))
            item['page'] = item_dict
            # yield item
            return item
    
    第 1 条附言  ·  2016-04-22 14:52:02 +08:00
    13 条回复    2016-04-23 00:22:19 +08:00
    naomhan
        1
    naomhan  
       2016-04-21 17:57:36 +08:00
    还是一样的啊 通过 css 选择权或者 xpath 取到正文所在区域 再去除干扰数据抽取正文
    icybee
        2
    icybee  
       2016-04-21 17:59:25 +08:00
    楼上说的对
    ChiChou
        3
    ChiChou  
       2016-04-21 18:21:42 +08:00
    跟进每一个链接再抓全部的内容。下面是爬乌云漏洞的例子,仅供参考

    ```python
    import re

    from scrapy.spiders import CrawlSpider, Rule
    from scrapy.linkextractors import LinkExtractor
    from scrapy.http import Request

    RE_VULID = r'\-(\d+\-\d+)'


    class WooyunSpider(CrawlSpider):
    name = 'wooyun'
    allowed_domains = ['www.wooyun.org']
    start_urls = ['http://www.wooyun.org/bugs/new_public/page/1']
    rules = (
    Rule(LinkExtractor(allow=(r'\/bugs\/new_public\/page\/\d+', ), )),
    Rule(LinkExtractor(allow=(r'\/bugs\/wooyun\-\d+\-\d+', )), callback='parse_vul'),
    )


    def __init__(self, *args, **kwarg):
    super(WooyunSpider, self).__init__(*args, **kwarg)
    self.finished = set()


    def make_requests_from_url(self, url):
    match = re.findall(RE_VULID, url)
    if match:
    vulid, = match
    if vulid in self.finished:
    return
    else:
    self.finished.add(vulid)

    return Request(url, dont_filter=False)


    def parse_vul(self, response):
    item = {key: ''.join([text.strip() for text in extracted]) for key, extracted in {
    'title': response.css('h3.wybug_title::text').re(ur'\t\t(\S+)'),
    'vulid': response.xpath('//h3/a[starts-with(@href,"/bugs/wooyun")]/@href').re(RE_VULID),
    'vendor': response.css('h3.wybug_corp a::text').extract(),
    'author': response.css('h3.wybug_author a::text').extract(),
    'submitted': response.css('h3.wybug_date::text').re('\t\t(\d+\-\d+\-\d+\s+\d+:\d+)'),
    'published': response.css('h3.wybug_open_date::text').re('\t\t(\d+\-\d+\-\d+\s+\d+:\d+)'),
    'detail': response.css('.wybug_detail').xpath('./node()').extract(),
    'patch': response.css('.wybug_patch .detail').xpath('./node()').extract(),
    'rank': response.css('.bug_result .detail').re(r'Rank[\s\S]?(\d*)'),
    'description': response.css('p.detail.wybug_description::text').extract(),
    'vultype': response.css('h3.wybug_type::text').re('\t\t(\S+)'),
    'level': response.css('.bug_result .detailTitle + p.detail::text').re(ur'\uff1a(\S+)'),
    }.iteritems()}

    yield item
    ```
    ChiChou
        4
    ChiChou  
       2016-04-21 18:22:18 +08:00
    所以特么怎么在回复里贴代码?
    v2014
        5
    v2014  
       2016-04-21 18:24:27 +08:00
    楼上都没理解楼主的意思,他的意思是取到了 url 和 title ,但是内容不在这个页面,怎么去爬内容。
    只要在 return 一个 Request(url, callback=self.parse),后面带个 callback 处理你的内容页就可以了,
    什么时候 return ,就看你是 bfs 还是 dfs 了
    bdbai
        6
    bdbai  
       2016-04-21 18:24:33 +08:00 via Android
    @ChiChou 贴 GitHub Gist 地址就可以,或者把空格转成全角空格。
    ChiChou
        7
    ChiChou  
       2016-04-21 20:14:49 +08:00
    @bdbai 并不想发 gist
    ChiChou
        8
    ChiChou  
       2016-04-21 20:15:12 +08:00
    @v2014 不用扯上我啊,我可是跟题主一模一样的场景
    Ixizi
        9
    Ixizi  
    OP
       2016-04-21 20:39:47 +08:00
    @ChiChou 淡定....
    ChiChou
        10
    ChiChou  
       2016-04-21 20:51:35 +08:00
    v2014
        11
    v2014  
       2016-04-22 12:20:01 +08:00
    @ChiChou 谁叫你打字比我快
    Ixizi
        12
    Ixizi  
    OP
       2016-04-22 14:54:37 +08:00
    @v2014
    @ChiChou

    我我我我我我我写出来了。。。代码应该还能优化,求指教
    ChiChou
        13
    ChiChou  
       2016-04-23 00:22:19 +08:00
    @v2014 怪我咯
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2472 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 16:10 · PVG 00:10 · LAX 09:10 · JFK 12:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.