马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 文静 于 2021-9-13 10:49 编辑
在解析百度地图的数据时遇到了xpath获取元素对应值反回空列表的情况
但是查看了网页源码其中有个看不懂的框架,看不懂.....import requests
from lxml import etree
latlng='34.264642646862,108.95108518068'
name='起点'
destination='34.364642646862,108.96108518068'
region='西安'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0',
}
url = 'http://api.map.baidu.com/direction?origin=latlng:{}|name:{}&destination={}&mode=driving®ion={}&output=html&src=webapp.baidu.openAPIdemo'.format(str(latlng),str(name),str(destination),str(region))
html = requests.get(url,headers=headers)
html_data = etree.HTML(html.content.decode())
distance = html_data.xpath('/html/body/div[1]/div[2]/ul[2]/li/div[1]/div[2]/div[1]/div[1]/div[1]/p[1]/span/text()')
print(distance)
使用了selenium发现可以获取到对应元素的值 import time
from selenium import webdriver
# driver = webdriver.Chrome(executable_path=r'C:\Users\Administrator\Desktop\HelloWorld\chromedriver_win32\chromedriver.exe')
driver = webdriver.Chrome()
driver.get(url)
# # 浏览器最大化
driver.maximize_window()
time.sleep(1)
# 这个webdriver的定位方法和浏览器xpath不一样,不能直接定位到标签的属性,需要首先定位到webelement,之后get到属性
i = driver.find_element_by_xpath('//*[@id="navtrans_content"]/div[1]/div[1]/div[1]/p[1]/span[1]').text
print(i)
# 保存为一张图片
driver.save_screenshot('百度.png')
本人小白不用太高深的回答,指点方向我自百度
这里是网页的那个啥....,网页源码太长了我截一点,不知道有没有用<script>
// pc和mobile端会稍有不同,必须严格按照该文档来部署
void function(a,b,c,d,e,f,g){a.alogObjectName=e,a[e]=a[e]||function(){(a[e].q=a[e].q||[]).push(arguments)},a[e].l=a[e].l||+new Date,d="https:"===a.location.protocol?"https://fex.bdstatic.com"+d:"http://fex.bdstatic.com"+d;var h=!0;if(a.alogObjectConfig&&a.alogObjectConfig.sample){var i=Math.random();a.alogObjectConfig.rand=i,i>a.alogObjectConfig.sample&&(h=!1)}h&&(f=b.createElement(c),f.async=!0,f.src=d+"?v="+~(new Date/864e5)+~(new Date/864e5),g=b.getElementsByTagName(c)[0],g.parentNode.insertBefore(f,g))}(window,document,"script","/hunter/alog/alog.min.js","alog"),void function(){function a(){}window.PDC={mark:function(a,b){alog("speed.set",a,b||+new Date),alog.fire&&alog.fire("mark")},init:function(a){alog("speed.set","options",a)},view_start:a,tti:a,page_ready:a}}();
void function(n){var o=!1;n.onerror=function(n,e,t,c){var i=!0;return!e&&/^script error/i.test(n)&&(o?i=!1:o=!0),i&&alog("exception.send","exception",{msg:n,js:e,ln:t,col:c}),!1},alog("exception.on","catch",function(n){alog("exception.send","exception",{msg:n.msg,js:n.path,ln:n.ln,method:n.method,flag:"catch"})})}(window);
</script>
数据应该是动态的
建议抓包,右键用不了,所以可以按F12
|