etree.html ()没数据,etree.html的用法问题

为什么在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"

看结果

ee02923046e98eb595553c1c2107d81e.png

代码里面用etree解析不到

d4cc44b4ff77c540153126cb8af17734.png

全部代码

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)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值