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

关于故宫紫禁城杂志 pdf 的爬取

  •  
  •   barnett2010 ·
    barnett2010 · 2022-09-08 21:59:37 +08:00 · 1827 次点击
    这是一个创建于 594 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这是总目录 www.dpm.org.cn/online_search/cid/1005847.html 这是单期目录 www.dpm.org.cn/onlines_detail/257998.html 这是单期里的单个 pdf www.dpm.org.cn/Uploads/File/2022/08/29/u630c57f072d3d.pdf

    我在脚本中运行下载,总是下载不到出来 pdf 。

    上来问问各位大佬,要怎么搞?

    如有不妥,管理可删。

    11 条回复    2022-09-25 19:40:45 +08:00
    jifengg
        1
    jifengg  
       2022-09-09 08:47:46 +08:00
    这我直接 wget 就能下载,查查代码吧。
    automation2022
        2
    automation2022  
       2022-09-09 16:53:03 +08:00
    这个应该不难的,我拿 clicknium 练手做了一个例子,供参考,
    https://github.com/automation9417/scrapy-bot-collection/tree/main/dpm
    barnett2010
        3
    barnett2010  
    OP
       2022-09-09 23:45:39 +08:00
    @automation2022 感谢大佬。就是想下载全部的 pdf 。我去研究一下你的代码。
    barnett2010
        4
    barnett2010  
    OP
       2022-09-10 00:13:42 +08:00
    不知有没有不需要用到 Visual Studio 软件就能执行的 python 代码
    automation2022
        5
    automation2022  
       2022-09-10 01:00:29 +08:00
    @barnett2010 vscode 只是开发工具,执行的时候是不依赖 vscode 的
    blankmiss
        6
    blankmiss  
       2022-09-10 01:40:14 +08:00
    我很奇怪 请求 总目录的链接 返回的 html 和页面额内容不一样
    zoofy
        8
    zoofy  
       2022-09-13 16:40:57 +08:00
    @barnett2010 简单写了个下载 pdf 的 demo, 单纯的 requests 请求. 可以自己优化一下

    import requests

    headers = {
    "Referer": "https://www.dpm.org.cn/Public/static/pdfwrap/js/pdf.worker.js",
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.33"
    }

    url = "https://www.dpm.org.cn/Uploads/File/2022/08/29/u630c38230f984.pdf"

    resp = requests.get(url, headers=headers)

    total_len = int(resp.headers["Content-Length"])

    range_int = 65535

    loop = total_len // range_int + 1

    for i in range(loop):
    if i == loop - 1:
    exist = True
    start = range_int * i + 1
    end = total_len
    elif i == 0:
    exist = False
    start = 0
    end = range_int
    else:
    exist = True
    start = range_int * i + 1
    end = range_int * (i + 1)

    headers["Range"] = f"bytes={start}-{end}"

    content = requests.get(url, headers=headers).content

    if not exist:
    with open("a.pdf", "wb") as f:
    f.write(content)
    else:
    with open("a.pdf", "ab") as f:
    f.write(content)
    barnett2010
        9
    barnett2010  
    OP
       2022-09-17 17:44:30 +08:00
    @zoofy 感谢大佬。我的 py 版本是 374 。在运行代码时有个这样的提示。
    elif i == 0:
    ^
    SyntaxError: invalid syntax
    但我看这句完全没错,怎么会有 bug ?
    barnett2010
        10
    barnett2010  
    OP
       2022-09-17 17:52:47 +08:00
    @automation2022
    cmd 运行 py app.py
    能弹出 chrome 窗口。
    但也有个报错

    clicknium.common.models.exceptions.UnreachableBrowserExtensionError: The browser's extension is not running, please install or turn on the extension.
    automation2022
        11
    automation2022  
       2022-09-25 19:40:45 +08:00
    @barnett2010 cicknium 目前是通过浏览器扩展的方式来操作的(可以通过如下的方式 https://www.clicknium.com/documents/tutorial/extensions/chromeextension ),或者通过脚本的方式安装 https://www.clicknium.com/documents/references/python/webdriver/webextension/install_or_update
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4060 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 00:58 · PVG 08:58 · LAX 17:58 · JFK 20:58
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.