使用 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 requestsurl = "https://example.com/"response = requests.get(url)print(response.url) # 打印获取的 URL
2. 使用 Selenium 库
from selenium import webdriverdriver = webdriver.Chrome()driver.get("https://example.com/")print(driver.current_url) # 打印当前 URL
3. 使用 Beautiful Soup 库
from bs4 import BeautifulSouphtml = requests.get(url).contentsoup = BeautifulSoup(html, "html.parser")for link in soup.find_all('a'): print(link.get('href')) # 打印所有链接的 URL
4. 使用 Urllib 库
from urllib.request import urlopenurl = "https://example.com/"with urlopen(url) as response: print(response.geturl()) # 打印获取的 URL
5. 使用 Scapy
from scapy.all import sr1resp = 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的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1347180.html
微信扫一扫
支付宝扫一扫