파이썬 파충류 - xpath

3711 단어 python 파충류
import  requests
from lxml import  etree
url = "https://news.baidu.com"
headers = {
     "User-Agent":"Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11"}
data = requests.get(url,headers=headers).content.decode()
#xpath  1、 /
#          2、   //
#          3、 //a[@ =“ ”]
#          4、 text()
#          5、 :@href
#          6、xpath 
# 
html_new=data.replace(r'','"')
xpath_data = etree.HTML(data)
# xpath 
with open("11.html","w")as f:
     f.write(data)

result = xpath_data.xpath('/html/head/title/text()')
result = xpath_data.xpath("//a/text()")
result1 = xpath_data.xpath('//a[@data-path="s?wd="]/text()')
#result = xpath_data.xpath('//a[@mon="target=appLink"]/@href')


print(result1)

좋은 웹페이지 즐겨찾기