報錯“The result of the xpath expression is: [object Attr]. It should be an element”
yutube爬蟲動態加載,需要用到selenium-webdriver,使用過程中,首先使用
find_elements_by_xpath進行批量標簽的定位選取,之后
使用find_element_by_xpath精細篩選選標簽的時候出現上面錯誤提示,
原因是這個webdriver的定位方法和瀏覽器xpath不一樣,不能直接定位到標簽的屬性
需要首先定位到webelement,之后get到屬性
正確
try: temp['host_url'] = node.find_element_by_xpath('./div/div/div/ytd-video-meta-block/div/div/div/yt-formatted-string/a').get_attribute('href') except Exception as e: print(e) try: temp['show_url'] = node.find_element_by_xpath('./div/ytd-thumbnail/a').get_attribute('href') except Exception as e: print(e) try: temp['title'] = node.find_element_by_xpath('./div/div/div[1]/div/h3/a').get_attribute('title') except Exception as e: print(e) try: temp['user'] = node.find_element_by_xpath('./div/div/div/ytd-video-meta-block/div/div/div/yt-formatted-string/a').text except Exception as e:
錯誤:
try: temp['host_url'] = node.find_element_by_xpath('./div/div/div/ytd-video-meta-block/div/div/div/yt-formatted-string/a/@href') except Exception as e: print(e) try: temp['show_url'] = node.find_element_by_xpath('./div/ytd-thumbnail/a/@href') except Exception as e: print(e) try: temp['title'] = node.find_element_by_xpath('./div/div/div[1]/div/h3/a/@title') except Exception as e: print(e) try: temp['user'] = node.find_element_by_xpath('./div/div/div/ytd-video-meta-block/div/div/div/yt-formatted-string/a/text()') except Exception as e: print(e)