1. 程式人生 > 實用技巧 >xpath選擇器的使用,selenium使用

xpath選擇器的使用,selenium使用

一、xpath選擇器使用

# xpath: XPath 是一門在 XML 文件中查詢資訊的語言
# / :從根節點選取。
# // :不管位置,直接找
# /@屬性名
# /text()
# 會複製()


doc='''
<html>
<head>
<base href='http://example.com/' />
<title>Example website</title>
</head>
<body>
<div id='images'>
<a href='image1.html' aa='bb'>Name: My image 1 <br /><img src='image1_thumb.jpg' /></a>
<a href='image2.html'>Name: My image 2 <br /><img src='image2_thumb.jpg' /></a>
<a href='image3.html'>Name: My image 3 <br /><img src='image3_thumb.jpg' /></a>
<a href='image4.html'>Name: My image 4 <br /><img src='image4_thumb.jpg' /></a>
<a href='image5.html' class='li li-item' name='items'>Name: My image 5 <br /><img src='image5_thumb.jpg' /></a>
<a href='image6.html' name='items'><span><h5>test</h5></span>Name: My image 6 <br /><img src='image6_thumb.jpg' /></a>
</div>
</body>
</html>
'''

from lxml import etree

html=etree.HTML(doc)
# html=etree.parse('search.html',etree.HTMLParser())
# 1 所有節點
# a=html.xpath('//*')

# 2 指定節點(結果為列表)
# a=html.xpath('//head')

# 3 子節點,子孫節點
# a=html.xpath('//div/a')
# a=html.xpath('//body/a') #無資料
# a=html.xpath('//body//a')


# 4 父節點
# a=html.xpath('//body//a[@href="image1.html"]/..')
# a=html.xpath('//body//a[1]/..')
# 也可以這樣
# a=html.xpath('//body//a[1]/parent::*')

# 5 屬性匹配
# a=html.xpath('//body//a[@href="image1.html"]')

# 6 文字獲取(重要) /text() 取當前標籤的文字
# a=html.xpath('//body//a[@href="image1.html"]/text()')
# a=html.xpath('//body//a/text()')

# 7 屬性獲取 @href 取當前標籤的屬性
# a=html.xpath('//body//a/@href')

# # 注意從1 開始取(不是從0)
# a=html.xpath('//body//a[1]/@href')
# 8 屬性多值匹配
# a 標籤有多個class類,直接匹配就不可以了,需要用contains
# a=html.xpath('//body//a[@class="li"]')
# a=html.xpath('//body//a[contains(@class,"li")]')
# a=html.xpath('//body//a[contains(@class,"li")]/text()')
# 9 多屬性匹配
# a=html.xpath('//body//a[contains(@class,"li") or @name="items"]')
# a=html.xpath('//body//a[contains(@class,"li") and @name="items"]/text()')
# a=html.xpath('//body//a[contains(@class,"li")]/text()')
# 10 按序選擇
# a=html.xpath('//a[2]/text()')
# a=html.xpath('//a[2]/@href')
# 取最後一個
# a=html.xpath('//a[last()]/@href')
# 位置小於3的
# a=html.xpath('//a[position()<3]/@href')
# 倒數第二個
# a=html.xpath('//a[last()-2]/@href')
# 11 節點軸選擇
# ancestor:祖先節點
# 使用了* 獲取所有祖先節點
# a=html.xpath('//a/ancestor::*')
# # 獲取祖先節點中的div
# a=html.xpath('//a/ancestor::div')
# attribute:屬性值
# a=html.xpath('//a[1]/attribute::*')
# a=html.xpath('//a[1]/@aa')
# child:直接子節點
# a=html.xpath('//a[1]/child::*')
# a=html.xpath('//a[1]/child::img/@src')
# descendant:所有子孫節點
# a=html.xpath('//a[6]/descendant::*')
# a=html.xpath('//a[6]/descendant::h5/text()')
# following:當前節點之後所有節點(兄弟節點和兄弟內部的節點)
# a=html.xpath('//a[1]/following::*')
# a=html.xpath('//a[1]/following::*[1]/@href')
# following-sibling:當前節點之後同級節點(只找兄弟)
# a=html.xpath('//a[1]/following-sibling::*')
# a=html.xpath('//a[1]/following-sibling::a')
# a=html.xpath('//a[1]/following-sibling::*[2]')
# a=html.xpath('//a[1]/following-sibling::*[2]/@href')


print(a)


# /
# //
# /@屬性名
# /text()

//以後去查詢標籤,bs4的find, css,xpath(通用的)

二、selenium使用

# 為了解決requests無法直接執行JavaScript程式碼的問題 
# 


# pip3 install selenium


# 瀏覽器驅動:http://npm.taobao.org/mirrors/chromedriver/
# 驅動要跟瀏覽器版本對應  84.0.4147.105:驅動用84.0.4147.30/
# 下載完解壓就是個exe(不同平臺的可執行檔案)
# from selenium import webdriver
# import time
# # bro=webdriver.Chrome()  # 得到一個谷歌瀏覽器物件,
# # 指定使用跟那個驅動
# bro=webdriver.Chrome(executable_path='./chromedriver.exe') # 得到一個谷歌瀏覽器物件,
#
# time.sleep(2)
# bro.get('https://www.baidu.com/')  # 在位址列裡輸入了百度
# time.sleep(2)
# print(bro.page_source)
# time.sleep(2)
# bro.close()



# 模擬登陸百度
# from selenium import webdriver
# import time
# bro=webdriver.Chrome(executable_path='./chromedriver.exe')
#
# bro.get('https://www.baidu.com/')
# time.sleep(0.01)
# input_k=bro.find_element_by_id('kw')
# input_k.send_keys('美女')  # 在框裡寫入美女
# time.sleep(2)
# sou=bro.find_element_by_id('su')  # 找到搜尋按鈕
# sou.click() # 點選搜尋按鈕
# time.sleep(4)
# bro.close()


# from selenium import webdriver
# import time
# bro=webdriver.Chrome(executable_path='./chromedriver.exe')
# bro.implicitly_wait(5)  # 隱士等待:找一個控制元件,如果控制元件沒有加載出來,等待5s中  等待所有,只需要寫著一句,以後找所有控制元件都按這個操作來
# bro.get('https://www.baidu.com/')
#
# d_button=bro.find_element_by_link_text('登入')
#
# d_button.click()
#
# login_u=bro.find_element_by_id('TANGRAM__PSP_11__footerULoginBtn')
# login_u.click()
#
# username=bro.find_element_by_id('TANGRAM__PSP_11__userName')
# username.send_keys('yxp654799481')
# password=bro.find_element_by_id('TANGRAM__PSP_11__password')
# password.send_keys('yxp997997')
# time.sleep(3)
# submit=bro.find_element_by_id('TANGRAM__PSP_11__submit')
#
# submit.click()
# time.sleep(10)
#
# print(bro.get_cookies())
#
# bro.close()



# ##############選擇器(find系列)
# ===============所有方法===================
# 1、find_element_by_id   # 通過id查詢控制元件
# 2、find_element_by_link_text  # 通過a標籤內容找
# 3、find_element_by_partial_link_text  # 通過a標籤內容找,模糊匹配
# 4、find_element_by_tag_name   # 標籤名
# 5、find_element_by_class_name  # 類名
# 6、find_element_by_name      # name屬性
# 7、find_element_by_css_selector  # 通過css選擇器
# 8、find_element_by_xpath       # 通過xpaht選擇器
# 強調:

# 1、find_elements_by_xxx的形式是查詢到多個元素,結果為列表




# 獲取元素屬性
# 重點
# tag.get_attribute('href')  # 找當前控制元件 的href屬性對的值
# tag.text   # 獲取文字內容

# 瞭解
# print(tag.id)   # 當前控制元件id號
# print(tag.location)  # 當前控制元件在頁面位置
# print(tag.tag_name)  # 標籤名
# print(tag.size)      #標籤的大小



####無介面瀏覽器(phantomjs)
#谷歌瀏覽器支援不開啟頁面
# from selenium.webdriver.chrome.options import Options
# from selenium import webdriver
# chrome_options = Options()
# chrome_options.add_argument('window-size=1920x3000') #指定瀏覽器解析度
# chrome_options.add_argument('--disable-gpu') #谷歌文件提到需要加上這個屬性來規避bug
# chrome_options.add_argument('--hide-scrollbars') #隱藏滾動條, 應對一些特殊頁面
# chrome_options.add_argument('blink-settings=imagesEnabled=false') #不載入圖片, 提升速度
#
#
# chrome_options.add_argument('--headless') #瀏覽器不提供視覺化頁面. linux下如果系統不支援視覺化不加這條會啟動失敗
#
#
# bro=webdriver.Chrome(chrome_options=chrome_options,executable_path='./chromedriver.exe')
# bro.get('https://www.baidu.com/')
# print(bro.page_source)
# bro.close()


######元素互動
# tag.send_keys()  # 往裡面寫內容
# tag.click()      # 點選控制元件
# tag.clear()      # 清空控制元件內容

#####執行js(有什麼用?)

# from selenium import webdriver
# import time
# bro=webdriver.Chrome(executable_path='./chromedriver.exe')
# bro.implicitly_wait(5)  # 隱士等待:找一個控制元件,如果控制元件沒有加載出來,等待5s中  等待所有,只需要寫著一句,以後找所有控制元件都按這個操作來
# bro.get('https://www.baidu.com/')
#
#
# bro.execute_script('window.open()')
# bro.execute_script('window.open()')
# time.sleep(2)
# bro.close()


####模擬瀏覽器前進後退

# from selenium import webdriver
# import time
# browser=webdriver.Chrome(executable_path='./chromedriver.exe')
# browser.get('https://www.baidu.com')
# browser.get('https://www.taobao.com')
# browser.get('http://www.sina.com.cn/')
#
# browser.back()
# time.sleep(1)
# browser.forward()
#
# browser.close()


#####獲取cookie
# bro.get_cookies()



#### 選項卡管理(瞭解)
# from selenium import webdriver
# import time
# browser=webdriver.Chrome()
# browser.get('https://www.baidu.com')
# browser.execute_script('window.open()')
#
# print(browser.window_handles) #獲取所有的選項卡
# browser.switch_to_window(browser.window_handles[1])
# browser.get('https://www.taobao.com')
# time.sleep(2)
# browser.switch_to_window(browser.window_handles[0])
# browser.get('https://www.sina.com.cn')
# browser.close()



##### 異常處理
# from selenium import webdriver
# from selenium.common.exceptions import TimeoutException,NoSuchElementException,NoSuchFrameException
# browser=webdriver.Chrome()
# try:
#
#     browser.get('')
# except Exception as e:
#     print(e)
# finally:
#     # 無論是否出異常,最終都要關掉
#     browser.close()



#####動作鏈()


#### 如何把螢幕拉倒最後(js控制)

# bro.execute_script('window.scrollTo(0,document.body.offsetHeight)')