使用 Python 爬虫查看 URL 有多种方法:使用 Requests 库,requests.get(url)使用 Selenium 库,driver.current_url使用 Beautiful Soup 库,解析 HTML 中的链接使用 Urllib 库,urlopen(url).geturl()使用 Scapy 库,检查 DNS 响应中的 URL

如何使用 Python 爬虫查看 URL
查看 URL 的方法
使用 Python 爬虫查看 URL 有多种方法,具体取决于所使用的库或框架。下面列出了一些常见方法:
1. 使用 Requests 库
立即学习“Python免费学习笔记(深入)”;
import requests url = "https://example.com/" response = requests.get(url) print(response.url) # 打印获取的 URL
2. 使用 Selenium 库
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://example.com/")
print(driver.current_url) # 打印当前 URL3. 使用 Beautiful Soup 库
from bs4 import BeautifulSoup
html = requests.get(url).content
soup = BeautifulSoup(html, "html.parser")
for link in soup.find_all('a'):
print(link.get('href')) # 打印所有链接的 URL4. 使用 Urllib 库
from urllib.request import urlopen
url = "https://example.com/"
with urlopen(url) as response:
print(response.geturl()) # 打印获取的 URL5. 使用 Scapy
from scapy.all import sr1 resp = sr1(IP(dst = "8.8.8.8")/UDP(dport=53)/DNS(rd=1, qd=DNSQR(qname="example.com."))) print(resp[DNS].an.rdata) # 打印 DNS 响应中的 URL











