etree-xpath


import requests
from lxml import etree
url = 'https://item.taobao.com/item.htm?spm=a219r.lm893.14.118.238e8d532nCpy9&id=537796877521&ns=1&abbucket=1'

response = requests.get(url)
html = etree.HTML(response.text)
# 1. 定位屬性為某值的標簽
sel_1 = html.xpath('//div[contains(@class,"tb-main-title")]]')
# 2. 定位屬性值是以某值開始的標簽
sel_2 = html.xpath('//h3[starts-with(@class,"tb-main-title")]')
# 3. 定位含有某文本的標簽
html.xpath('//h3[contains(text(),"小白")]')
# 4. 定位文本為特定值的標簽
html.xpath('//span[text()="價格"]')[0].text


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM