用Python 爬虫批量下载PDF文档

本文介绍如何使用Python爬虫批量下载PDF文档,通过分析网页源代码获取实际PDF链接,实现自动化下载。示例包括从不同类型的网页结构中提取PDF地址并下载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

更新:之前代码是用 python2 写的,有关 python3 的代码可以参考这位博主的:https://blog.youkuaiyun.com/baidu_28479651/article/details/76158051

代码如下:

# coding = UTF-8
# 爬取李东风PDF文档,网址:http://www.math.pku.edu.cn/teachers/lidf/docs/textrick/index.htm

import urllib.request
import re
import os

# open the url and read
def getHtml(url):
    page = urllib.request.urlopen(url)
    html = page.read()
    page.close()
    return html

# compile the regular expressions and find
# all stuff we need
def getUrl(html):
    reg = r'(?:href|HREF)="?((?:http://)?.+?\.pdf)'
    url_re = re.compile(reg)
    url_lst = url_re.findall(html.decode('gb2312'))
    return(url_lst)

def getFile(url):
    file_name = url.split('/')[-1]
    u = urllib.request.urlopen(url)
    f 
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值