【问题标题】:xpath does not work with this site, pls verifyxpath 不适用于此站点,请验证
【发布时间】:2014-09-27 04:11:00
【问题描述】:

我使用 Python 和 selenium (PhantomJS webdriver) 来解析网站,但我遇到了问题。

我想从这个广播网站获取当前歌曲:http://www.eskago.pl/radio/eska-warszawa

xpath:

/html/body/div[3]/div[1]/section[2]/div/div/div[2]/ul/li[2]/a[2]

xpath 不适用于 python selenium

错误:

Traceback(最近一次调用最后一次):文件“parser4.py”,第 41 行,在 p.loop() 文件“parser4.py”,第 37 行,循环中 self.eska(self.url_eskawarszawa) 文件“parser4.py”,第 27 行,在 eska driver.find_element_by_xpath('/html/body/div[3]/div[1]/section[2]/div/div/div[2]/ul/li[2]/a[2]') 文件 "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", 第 230 行,在 find_element_by_xpath 中 return self.find_element(by=By.XPATH, value=xpath) 文件“/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py”, 第 662 行,在 find_element 中 {'using': by, 'value': value})['value'] 文件“/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py”, 第 173 行,执行中 self.error_handler.check_response(response) 文件“/usr/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py”, 第 164 行,在 check_response 中 raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: 消息: u'{"errorMessage":"无法使用 xpath 找到元素 \'/html/body/div[3]/div[1]/section[2]/div/div/div[2]/ul/li[2]/a[2]\'","re​​quest": {"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"148","Content-Type": "application/json;charset=UTF-8","Host":"127.0.0.1:55583","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method" :"POST","post":"{\"使用\": \"xpath\"、\"sessionId\": \"e2fa7700-1bea-11e4-bd11-83e129ae286e\", \"值\": \"/html/body/div[3]/div[1]/section[2]/div/div/div[2]/ul/li[2]/a[2]\"}","url" :"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element", "relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"" ,"protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/e2fa7700-1bea-11e4-bd11 -83e129ae286e/元素"}}' ;屏幕截图:可通过屏幕获得

有人知道这有什么问题吗?

--------------------------------------

编辑: 谢谢大家的回答 我终于找到了解决我的问题的方法。 xpath 很好(但实际上很脆弱)

我使用 firefox 驱动程序,但我看到了问题 - 广告。

我将不得不跳过它们,我决定使用另一个没有此广告的页面: http://www.eskago.pl/radio

最后,谢谢 alecxe - 我用这个:

driver.find_element_by_xpath('//a[@class="radio-tab-button"]/span/strong').click()
element = driver.find_element_by_xpath('//p[@class="onAirStreamId_999"]/strong')
print element.text

完美运行。

【问题讨论】:

  • Unable to find element with xpath \'/html/body/div[3]/div[1]/section[2]/div/div/div[2]/ul/li[2]/a[2]\'
  • 你知道那条消息是什么意思吗?
  • 您需要学习如何编写正确的 XPath。试试这个:zvon.org/xxl/XPathTutorial/General/examples.html
  • @Siking,你可能是对的,但如果没有更多的具体细节,很难遵循这种建议。您认为需要改进的 XPath 表达式有哪些方面?
  • @LarsH 很明显,OP 甚至不知道 XPath 是什么,更不用说如何构造一个了; OP还没有做最基本的研究。很抱歉,我还不够好,甚至无法用 600 个字符或更少的字符教别人 XPath 的基础知识,只能向他们指出我自己几年前开始的教程。

标签: python parsing selenium xpath selenium-webdriver


【解决方案1】:

您提供的 xpath 非常脆弱,现在想知道您是否收到了 NoSuchElementException 异常。

而是依靠a标签的类名,里面有当前正在播放的歌曲:

<a class="playlist_small" href="http://www.eskago.pl/radio/eska-warszawa?noreload=yes">
    <img style="width:41px;" src="http://t-eska.cdn.smcloud.net/common/l/Q/s/lQ2009158Xvbl.jpg/ru-0-ra-45,45-n-lQ2009158Xvbl_jessie_j_bang_bang.jpg" alt="">
    <strong>Jessie J, Ariana Grande, Nicki Minaj</strong>
    <span>Bang Bang</span>
</a>

示例代码如下:

element = driver.find_element_by_xpath('//a[@class="playlist_small"]/strong')
print element.text

嗯,另一种检索当前播放歌曲的方法 - 是模仿网站为播放列表做出的 JSONP 响应:

>>> import requests
>>> import json
>>> import re
>>> response = requests.get('http://static.eska.pl/m/playlist/channel-999.jsonp')
>>> json_data = re.match('jsonp\((.*?)\);', response.content).group(1)
>>> songs = json.loads(json_data)
>>> current_song = songs[0]
>>> [artist['name'] for artist in current_song['artists']]
[u'David Guetta', u'Showtek', u'Vassy']
>>> current_song['name']
u'Bad'

【讨论】:

  • @OP: 或使用 CSS..a.playlist_small &gt; strong
  • 我认为您的第二个代码示例无效。 rejson 是什么?
  • 谢谢,我终于找到了使用 xpath 的解决方案,但是您使用 jsonp 编写的第二个代码更有趣,我认为这是最快的方法。我现在不知道那个 jsonp 的东西是什么,但我很好奇你从哪里得到这个与 jsonp 的链接?
  • @malloc 我刚刚使用了浏览器开发工具并检查了所有发出的请求。
【解决方案2】:

正如 alecxe 所说,如果页面结构发生任何变化,xpath 就会中断。

一个更简单的 xpath 表达式将起作用://li[2]/a[2]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多