为什么在chrome调试工具里面可以定位到的xpath,在etree.html解析不出来
url:https://longbridge.global/news/
xpath:"//*[@id="__layout"]/div/div[2]/div/div/div/div[1]/div[2]/div[2]/div[2]/div"
看结果
代码里面用etree解析不到
全部代码
import requests
from lxml import etree
# 获取网页源码 并以文本形式返回
def get_html(url):
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36"}
try:
html = requests.get(url=url, headers=headers)
html.encoding = html.apparent_encoding
if html.status_code == 200:
print('成功获取源码')
except Exception as e:
print('获取失败:%s' % e)
return html.text
# 分析html 操作xpath爬取关键元素,以键值对形式返回
def parse_html(html):
all_info = []
html = etree.HTML(html)
lis = html.xpath("//*[@id='__layout']/div/div[2]/div/div/div/div[1]/div[2]/div[2]/div[2]/div")
print(len(lis))
# for li in lis:
# title = \
# li.xpath("//*[@id=\"__layout\"]/div/div[2]/div/div/div/div[1]/div[2]/div[2]/div[2]/div[1]/div/div/div[1]/a[0]/text()")
# content = li.xpath("//*[@id=\"__layout\"]/div/div[2]/div/div/div/div[1]/div[2]/div[2]/div[2]/div[1]/div/div/div[1]/a[1]")
# news_from = li.xpath("//*[@id=\"__layout\"]/div/div[2]/div/div/div/div[1]/div[2]/div[2]/div[2]/div[1]/div/div/div[2]/span")
# print(type(title))
# print(title, content, news_from)
# one_info = [title, content, news_from]
# all_info.append(one_info)
# return all_info
if __name__ == '__main__':
text = get_html("https://longbridge.global/news/")
parse_html(text)