python獲取href
A. python怎樣獲取XPath下的A標簽的內容
1、在瀏覽器中打開網頁。然後//div,就可以找到頁面中的所有div了,因為//表示的是任意目錄下查找。如圖,頁面有兩個div,所以可以找到兩個。
6、斜杠/表示的是獲取子元素,比如div下面有兩個子元素p,那麼//div[2]/p獲取的就是第二個div的子元素p。
B. python 正則如何抓取 <a></a> 中 href 屬性和標簽里的內容
importre
pattern='<a.*?href="(.+)".*?>(.*?)</a>'
withopen("test.html","r")asfp:
forlineinfp:
ret=re.search(pattern,line)
ifret:
forxinret.groups():printx
不知道具體格式是怎樣的,我這里也就簡單舉個例子。
groups獲取到的就是正則pattern裡面( )中的內容,以元組形式返回。
C. 用python selenium提取網頁中的所有<a>標簽中的超級鏈接地址
提取所有鏈接應該用循環:
urls=driver.find_elements_by_xpath("//a")
forurlinurls:
print(url.get_attribute("href"))
如果get_attribute方法報錯應該是沒有找到a標簽對象,如果確定是有的話,可能是頁面載入比較慢還沒載入出來,selenium默認是不會等待對象出現的,需要在找對象前加一些等待時間;另外如果頁面上有iframe的話需要先切換進去才能找到裡面的對象。
D. 如何獲取href標簽中的內容 python
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
html='''<dict>
<key>LogType</key>
<string>Default</string>
<key>Message</key>
<string>測試場景:訂餐提交頁面</string>
<string>Loop in : 2 rTime:0.266s</string>
<key>Timestamp</key>
<date>2014-06-06T12:16:24Z</date>
<key>Type</key>
<integer>1</integer>
</dict>'''
soup= BeautifulSoup(html)
trs=soup.findAll("string")
length=len(trs)
arr=[]
for i in range(length):
print trs[i].contents
需要安裝BeautifulSoup,代碼很容易懂的!
E. python怎麼獲取div下的ul下的li下的a里href的內容
利用request獲取網頁內容;
利用BeautifulSoup處理並獲取節點信息。
Python代碼
若沒有上述兩個模塊就用pip等工具安裝到python庫中
F. 如何用python抓取這個網頁的內容
如果包含動態內容可以考慮使用Selenium瀏覽器自動化測試框架,當然找人有償服務也可以
G. python BeautifulSoup 取class中的href怎麼寫
它就是當前節點的一個屬性,content["href"]
H. python 如何獲取url信息
importweb
defmake_text(string):
returnstring
urls=('/','tutorial')
render=web.template.render('templates/')
app=web.application(urls,globals())
my_form=web.form.Form(
web.form.Textbox('',class_='textfield',id='textfield'),
)
classtutorial:
defGET(self):
form=my_form()
returnrender.tutorial(form,"Yourtextgoeshere.")
defPOST(self):
form=my_form()
form.validates()
s=form.value['textfield']
returnmake_text(s)
if__name__=='__main__':
app.run()
I. python怎麼獲取動態網頁鏈接
四中方法:
'''
得到當前頁面所有連接
'''
import requests
import re
from bs4 import BeautifulSoup
from lxml import etree
from selenium import webdriver
url = 'http://www.ok226.com'
r = requests.get(url)
r.encoding = 'gb2312'
# 利用 re
matchs = re.findall(r"(?<=href=\").+?(?=\")|(?<=href=\').+?(?=\')" , r.text)
for link in matchs:
print(link)
print()
# 利用 BeautifulSoup4 (DOM樹)
soup = BeautifulSoup(r.text,'lxml')
for a in soup.find_all('a'):
link = a['href']
print(link)
print()
# 利用 lxml.etree (XPath)
tree = etree.HTML(r.text)
for link in tree.xpath("//@href"):
print(link)
print()
# 利用selenium(要開瀏覽器!)
driver = webdriver.Firefox()
driver.get(url)
for link in driver.find_elements_by_tag_name("a"):
print(link.get_attribute("href"))
driver.close()
J. python 用自定義函數獲取所有超鏈接
用正則匹配
importre
html='''<h3>contactus</h3>
<p>contact:managerwang</p>
<p>telephone:12345666</p>
<divid="nav">
<ul>
<li><aclass="nav-first"href="/">homepage</a></li>
<li><ahref="/lista.php">111</a></li>
<li><ahref="/lista.php">222</a></li>
<li><ahref="/order/setorder.php">333</a></li>
<li><ahref="/what/cool/ista.php">444</a></li>
</ul>
</div>'''
urls=re.findall('href=.*?>',html)#正則出a鏈接href
urlList=[]#定義urlList
forurlinurls:
url=url.replace("href="",'')#替換href="
urlList.append(url[:-2])#獲取的0到-2長度的字元串
print(urlList)
輸出:
['/','/lista.php','/lista.php','/order/setorder.php','/what/cool/ista.php']