xpath在该网站无法使用,请确认
我在用Python和selenium(PhantomJS驱动)解析网站,但遇到了一些问题。
我想从这个电台网站获取当前播放的歌曲: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中不管用。
错误信息:
追踪信息(最近的调用在最前面): 文件 "parser4.py",第41行,在 p.loop() 文件 "parser4.py",第37行,在 loop 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行,在 execute 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]\'","request":{"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":"{\"using\": \"xpath\", \"sessionId\": \"e2fa7700-1bea-11e4-bd11-83e129ae286e\", \"value\": \"/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/element"}}' ; 截图:可通过屏幕获取
有没有人知道这是什么问题?
--------------------------------------
编辑: 谢谢大家的回答 我终于找到了问题的解决办法。 这个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
结果运行得很好。
2 个回答
正如alecxe提到的,如果页面的结构发生任何变化,那个xpath就会失效。
一个更简单的xpath表达式是: //li[2]/a[2]
你提供的这个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'