Python爬虫——xpath

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)

你可能感兴趣的:(python爬虫)