python爬虫怎么查看url

当前位置: 电视猫 > php教程>
电视猫时间: 2023-11-30 15:00:00

  python爬虫怎么查看url

使用 python 爬虫查看 url 有多种方法:使用 requests 库,requests.get(url)使用 selenium 库,driver.current_url使用 beautiful soup 库,解析 html 中的链接使用 urllib 库,urlopen(url).geturl()使用 scapy 库,检查 dns 响应中的 url

python爬虫怎么查看url

如何使用 Python 爬虫查看 URL

查看 URL 的方法

使用 Python 爬虫查看 URL 有多种方法,具体取决于所使用的库或框架。下面列出了一些常见方法:

1. 使用 Requests 库

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)  # 打印当前 URL
登录后复制

3. 使用 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'))  # 打印所有链接的 URL
登录后复制

4. 使用 Urllib 库

from urllib.request import urlopen

url = "https://example.com/"
with urlopen(url) as response:
    print(response.geturl())  # 打印获取的 URL
登录后复制

5. 使用 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
登录后复制

以上就是python爬虫怎么查看url的详细内容,更多请关注php中文网其它相关文章!

最新电视剧
热门电视剧
影视资讯
最新剧情排行榜
最新电视剧剧情