python爬虫中用xpath无法获取某些标签问题
selenium 斗鱼直播页面
某个div标签下面的img标签在代码中无法用xpath获取到

xpath路径没有问题

代码中无法找到报错
def prase_data(self):
room_list = self.driver.find_elements_by_xpath('//*[@id="listAll"]/section[2]/div[2]/ul/li/div')
#print(len(room_list))
for room in room_list:
temp = {}
temp['title'] = room.find_element_by_xpath('./a[1]/div[2]/div[1]/h3').text
temp['type'] = room.find_element_by_xpath('./a[1]/div[2]/div[1]/span').text
temp['owner'] = room.find_element_by_xpath('./a[1]/div[2]/div[2]/h2').text
temp['num'] = room.find_element_by_xpath('./a[1]/div[2]/div[2]/span').text
temp['pic'] = room.find_element_by_xpath('./a[1]/div[1]/div[1]/img').get_attribute('src')
print(room)
#print(temp)

现在查看上一级标签内的内容
print( room.find_element_by_xpath('./a[1]/div[1]/div[1]').get_attribute('innerHTML'))
得到结果,发现得到的内容多了一层div

修改原来的代码再加一层div即可获取成功了
temp['pic'] = room.find_element_by_xpath('./a[1]/div[1]/div[1]/div[1]/img').get_attribute('src')
在selenium中遇到斗鱼直播页面的一个div标签下,通过xpath无法获取到img标签。确认xpath路径正确后,发现实际上是由于实际内容比预期多了一层div。通过调整代码,增加一层div的查找,最终成功获取到img标签。
58万+





