本文講述了 python 獲取當前頁面內的所有鏈結的五種方法,分享給大家僅供參考,具體如下:
# 利用 requests_htmlfrom requests_html import htmlsession
session = htmlsession()
url = ''
r = session.get(url)
print(r.html.links)
print('*'*100)
# 利用 beautifulsoup
import requests
from bs4 import beautifulsoup
url = ''
res = requests.get(url)
soup = beautifulsoup(res.text, 'lxml')
for a in soup.find_all('a'):
print(a['href'])
print('*'*100)
# 利用 re (不推薦用正則,太麻煩)
# 利用 lxml.etree
from lxml import etree
tree = etree.html(r.text)
for link in tree.xpath('//@href'):
print(link)
print('*'*100)
# 利用 selenium
from selenium import webdriver
chrome_options = webdriver.chromeoptions()
chrome_options.add_argument('--headless')
browser = webdriver.chrome(chrome_options=chrome_options)
browser.get(url)
for link in browser.find_elements_by_tag_name('a'):
print(link.get_attribute('href'))
c 獲取當前頁面URl
2 通過js獲取 thisdloc document.location thisurl document.url thishref document.location.href thissloc self.location.href thistloc top.location.href thispl...
獲取當前頁面的URL
window location host 返回url 的主機部分,例如 www.com window location hostname 返回www.com window location href 返回整個url字串 window location pathname 返回 a index.php或...
059 獲取當前頁面位址
1.window.location物件可用於獲取當前頁面位址 url 並把瀏覽器重定向到新頁面。2.window.location.href屬性返回當前頁面的url。3.window.location.hostname屬性返回 當前頁面的 網際網路主機的名稱。4.window.location.pa...