如何获取重定向的URL?

2024-10-01 02:31:30 发布

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

我正在尝试获取https://trade.ec.europa.eu/doclib/html/153814.htm指向的重定向URL(pdf文件)

我已经试过了

r = requests.get('https://trade.ec.europa.eu/doclib/html/153814.htm', allow_redirects = True)
print(r.url) 

并且它输出相同的旧URL。我需要重定向的URL,它是https://trade.ec.europa.eu/doclib/docs/2015/september/tradoc_153814.pdf


Tags: 文件httpsurlgetpdfhtmlrequests重定向
2条回答

我认为你应该自己获得一个重定向链接(没有找到任何方法来使用重定向),当你输入https://trade.ec.europa.eu/doclib/html/153814.htm时,它会给你一个带有重定向链接的HTML页面,例如你可以像这样提取它

import requests
from lxml import etree, html

tree = html.fromstring(requests.get('https://trade.ec.europa.eu/doclib/html/153814.htm').text)
print(tree.xpath('.//a/@href')[0])

输出将是

https://trade.ec.europa.eu/doclib/docs/2015/september/tradoc_153814.pdf

请尝试此代码,看看它是否适合您

import urllib.request
import re
import requests
import PyPDF2
import io
from requests_html import HTMLSession
from urllib.parse import urlparse
from PyPDF2 import PdfFileReader
 
# Get Domain Name With urlparse
url = "https://trade.ec.europa.eu/doclib/html/153814.htm"
parsed_url = urlparse(url)
domain = parsed_url.scheme + "://" + parsed_url.netloc
 
# Get URL 
session = HTMLSession()
r = session.get(url)
 
# Extract Links
jlinks = r.html.xpath('//a/@href')
 
# Remove bad links and replace relative path for absolute path
updated_links = []
 
for link in jlinks:
    if re.search(".*@.*|.*javascript:.*|.*tel:.*",link):
        link = ""
    elif re.search("^(?!http).*",link):
        link = domain + link
        updated_links.append(link)
    else:
        updated_links.append(link)
r = requests.get(updated_links[0])
f = io.BytesIO(r.content)
reader = PdfFileReader(f)
contents = reader.getPage(0).extractText() 
print(contents)

相关问题 更多 >