import requests
from bs4 import BeautifulSoup
import threading
import time
import urllib.request
url = 'http://www.mee.gov.cn/hjzl/dqhj/cskqzlzkyb/'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}
class DouYinMusic:
def __init__(self):
self.download_path()
@staticmethod#静态方法
def download_path(self=None):
global url
global headers
r = requests.get(url,headers=headers)
r.encoding = 'UTF-8'
soup = BeautifulSoup(r.text, 'html.parser')
for tag_div in soup.findAll(True, {'class': 'main_rt_list'}):
for tag_a in tag_div.find_all('a'):
print('tag_a', tag_a.get('href')[2:])
print('tag_a', tag_a.text)
t = threading.Thread(target=self.download_pdf, args=(tag_a))
time.sleep(0.5)
t.start()
def download_pdf(tag_a):
u = urllib.request.urlopen(url + tag_a.get('href')[2:])
f = open('C://Users//m//Desktop//down//' + tag_a.text + '.pdf', 'wb')
block_sz = 8192
while True:
buffer = u.read(block_sz)
if not buffer:
break
print('=====结束==========')
f.write(buffer)
f.close()
if __name__ == '__main__':
main = DouYinMusic()
#main.run()
使用python下载文件-PDF
最新推荐文章于 2025-10-13 14:40:36 发布
本文介绍了一个使用Python实现的网页爬虫项目,该项目通过requests和BeautifulSoup库抓取指定网站上的链接,并利用多线程技术同时下载多个PDF文件。文章详细展示了如何设置请求头、解析网页内容、提取链接以及使用线程池进行高效下载。
部署运行你感兴趣的模型镜像
您可能感兴趣的与本文相关的镜像
Python3.9
Conda
Python
Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本
929

被折叠的 条评论
为什么被折叠?



