推荐学习书目
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
Fuyu0gap
V2EX  ›  Python

scrapy 抓一个页面的 PDF,并且把他们合并为一个,但是发现文件大小对的,但是里面的内容只有第一个 PDF 里面的,代码如下,求教大神。

  •  
  •   Fuyu0gap · Apr 20, 2018 · 2339 views
    This topic created in 2974 days ago, the information mentioned may be changed or developed.

    代码如下:

    -- coding: utf-8 --

    import scrapy

    class LinearSpider(scrapy.Spider): name = "linear" allowed_domains = ["ocw.mit.edu"] start_urls = ['https://ocw.mit.edu/courses/mathematics/18-06sc-linear-algebra-fall-2011/resource-index/']

    def parse(self, response):
        page_hrefs = response.xpath("*//tr//td/a/@href").re(".*sum.pdf")
        for href in page_hrefs:
            new_url = 'https://ocw.mit.edu' + href
            print(new_url)
            yield scrapy.Request(new_url,callback=self.parse_href)
    
    def parse_href(self,response):
        with open('linear.pdf','ab') as f:
            f.write(response.body)
            f.close()
    
    3 replies    2018-04-23 17:20:53 +08:00
    Fuyu0gap
        1
    Fuyu0gap  
    OP
       Apr 20, 2018
    分开下载页面里所有的 PDF 是可以的,以及这个 Markdown 显示不全怎么肥四……
    pc10201
        2
    pc10201  
       Apr 21, 2018
    with open('linear.pdf','ab') as f
    pdf 不能这么简单的拼接吧,最好每一个分开下载,再用第三方工具整合在一起
    Fuyu0gap
        3
    Fuyu0gap  
    OP
       Apr 23, 2018
    @pc10201 我最后也是和你一样的思路解决的,不过 PDF 的拼接原理和文本差异在哪里呢?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5212 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 196ms · UTC 08:20 · PVG 16:20 · LAX 01:20 · JFK 04:20
    ♥ Do have faith in what you're doing.