下载不带.pdf u的pdf文件

2024-10-02 12:38:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从this website下载PDF文件。你知道吗

我是Python新手,目前正在学习Python软件。我下载了urllib和bs4等包。但是,在任何URL中都没有.pdf扩展名。相反,每个都有以下格式:http://www.smv.gob.pe/ConsultasP8/documento.aspx?vidDoc={.....}。你知道吗

我试过使用汤。全部找到命令。然而,这并不成功。你知道吗

from urllib import request
from bs4 import BeautifulSoup
import re
import os
import urllib

url="http://www.smv.gob.pe/frm_hechosdeImportanciaDia?data=38C2EC33FA106691BB5B5039DACFDF50795D8EC3AF"
response = request.urlopen(url).read()
soup= BeautifulSoup(response, "html.parser")    
links = soup.find_all('a', href=re.compile(r'(http://www.smv.gob.pe/ConsultasP8/documento.aspx?)'))
print(links)

Tags: fromimportrehttprequestwwwurllibpe
1条回答
网友
1楼 · 发布于 2024-10-02 12:38:50

这对我很有用:

import re

import requests
from bs4 import BeautifulSoup

url = "http://www.smv.gob.pe/frm_hechosdeImportanciaDia?data=38C2EC33FA106691BB5B5039DACFDF50795D8EC3AF"
response = requests.get(url).content
soup = BeautifulSoup(response, "html.parser")
links = soup.find_all('a', href=re.compile(r'(http://www.smv.gob.pe/ConsultasP8/documento.aspx?)'))
links = [l['href'] for l in links]
print(links)

唯一的区别是我使用requests是因为我已经习惯了,并且我为从BeautifulSoup返回的每个Tag使用href属性。你知道吗

相关问题 更多 >

    热门问题