1. 使用`requests`库:
import requests
url = "https://example.com/"
response = requests.get(url)
print(response.url) 输出真实的URL
2. 使用`BeautifulSoup`库:
from bs4 import BeautifulSoup
import requests
url = "https://example.com/"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
for link in soup.find_all("a"):
print(link.get("href")) 输出每个链接的真实URL
3. 使用`urllib`库:
from urllib.request import urlopen
url = "https://www.example.com"
response = urlopen(url)
print(response.geturl()) 输出真实的URL
4. 使用`lxml`库和XPath表达式:
from lxml import etree
url = "https://example.com/"
response = requests.get(url)
tree = etree.HTML(response.text)
for link in tree.xpath("//a/@href"):
print(link) 输出每个链接的真实URL
5. 使用正则表达式匹配URL:
import re
text = "This is a URL: https://example.com"
urls = re.findall(r"https?://\S+", text)
print(urls) 输出匹配到的URL列表
以上方法都可以用来获取网页中的真实URL。选择哪种方法取决于你的具体需求和上下文