Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml xpath表达式的结果是object,应该是元素_Xml_Selenium_Xpath_Webdriver_Screen Scraping - Fatal编程技术网

Xml xpath表达式的结果是object,应该是元素

Xml xpath表达式的结果是object,应该是元素,xml,selenium,xpath,webdriver,screen-scraping,Xml,Selenium,Xpath,Webdriver,Screen Scraping,我对抓取网站还不熟悉,我一直在尝试使用python抓取谷歌图片(最终是为了制作一个不和谐的机器人,但这并不重要)。我编写了以下代码,将图像src存储为列表,以便选择索引并显示图像(我使用xpath helper chrome扩展对其进行了测试,它返回了我需要的内容): 我得到的错误如下: Traceback (most recent call last): File "C:\Users\user\Desktop\tessst.py", line 8, in <module>

我对抓取网站还不熟悉,我一直在尝试使用python抓取谷歌图片(最终是为了制作一个不和谐的机器人,但这并不重要)。我编写了以下代码,将图像src存储为列表,以便选择索引并显示图像(我使用xpath helper chrome扩展对其进行了测试,它返回了我需要的内容):

我得到的错误如下:

Traceback (most recent call last):
  File "C:\Users\user\Desktop\tessst.py", line 8, in <module>
    review = driver.find_elements_by_xpath("//div[@jscontroller ='Q7Rsec']/a/img/@src")
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 410, in find_elements_by_xpath
    return self.find_elements(by=By.XPATH, value=xpath)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1007, in find_elements
    'value': value})['value'] or []
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: The result of the xpath expression "//div[@jscontroller ='Q7Rsec']/a/img/@src" is: [object Attr]. It should be an element.
  (Session info: chrome=73.0.3683.75)
  (Driver info: chromedriver=73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72),platform=Windows NT 10.0.17134 x86_64)

我刚刚想到nltk没有被使用(我在某个时候玩过它,忘了删除它)

没关系,我很愚蠢,只是现在才注意到你的URL在脚本中;我可以确认xpath正在查找
img
元素的
src
属性。下面应该找到该元素,然后获取其src属性

review = driver.find_elements_by_xpath("//div[@jscontroller ='Q7Rsec']/a/img")
这将返回100个元素。我不确定您想对源代码做什么,但这里有一些代码可以简单地打印每个源代码:

for x in review
    if x.get_attribute.src != "":
        print(x.get_attribute(“src”)

这应该为指定了
src
的55个元素打印
src
属性。

没关系,我很笨,只是现在才注意到你的URL在脚本中;我可以确认xpath正在查找
img
元素的
src
属性。下面应该找到该元素,然后获取其src属性

review = driver.find_elements_by_xpath("//div[@jscontroller ='Q7Rsec']/a/img")
这将返回100个元素。我不确定您想对源代码做什么,但这里有一些代码可以简单地打印每个源代码:

for x in review
    if x.get_attribute.src != "":
        print(x.get_attribute(“src”)

这应该为指定了
src
的55个元素打印
src
属性。

您不能在xpath中添加您缺少的
src
属性。但是,我观察到一些图像没有
src
属性。相反,它具有
数据src
属性。这是您的解决方案。希望这有帮助

from selenium import webdriver
from selenium.webdriver.common.by import By
chrome_path =r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
chromedriver_path = r'C:\Users\user\Desktop\chromedriver.exe'
driver = webdriver.Chrome(chromedriver_path)
driver.get("https://www.google.com/search?q=pepega&rlz=1C1GIWA_enGB617GB617&source=lnms&tbm=isch&sa=X&ved=0ahUKEwjH1e6b-YfhAhWRs3EKHeKmAqoQ_AUIDigB&biw=2560&bih=947")


reviews = driver.find_elements_by_xpath("//div[@jscontroller ='Q7Rsec']/a/img")

list_review=[]
for review in reviews:
   if review.get_attribute("src") is not None:
        list_review.append(review.get_attribute("src"))
print(list_review)
print(len(list_review))

您不能在xpath中添加缺少的
src
属性。但是,我观察到一些图像没有
src
属性。相反,它具有
data src
属性。这是您的解决方案。希望这对您有所帮助

from selenium import webdriver
from selenium.webdriver.common.by import By
chrome_path =r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
chromedriver_path = r'C:\Users\user\Desktop\chromedriver.exe'
driver = webdriver.Chrome(chromedriver_path)
driver.get("https://www.google.com/search?q=pepega&rlz=1C1GIWA_enGB617GB617&source=lnms&tbm=isch&sa=X&ved=0ahUKEwjH1e6b-YfhAhWRs3EKHeKmAqoQ_AUIDigB&biw=2560&bih=947")


reviews = driver.find_elements_by_xpath("//div[@jscontroller ='Q7Rsec']/a/img")

list_review=[]
for review in reviews:
   if review.get_attribute("src") is not None:
        list_review.append(review.get_attribute("src"))
print(list_review)
print(len(list_review))

你能在你的问题中编辑你的网页或其URL的完整HTML吗?这是我所知道的对xPath进行故障排除的唯一方法。xPath必须找到一个属性;我怀疑罪魁祸首可能是
@src
。您希望在该页面上显示多少src?当SRC不可用时,您是否只考虑SRC的元素或想添加数据SRC?您可以将您的网页或URL的完整HTML编辑到您的问题中吗?这是我所知道的对xPath进行故障排除的唯一方法。xPath必须找到一个属性;我怀疑罪魁祸首可能是
@src
。您希望在该页面上显示多少src?当SRC不可用时,您是否只考虑带有SRC的元素或想添加数据SRC?嗨,今天我试过了,它说WebEnter对象没有属性SRC。我不确定这是否有帮助,但我打印了xpath更改后正在审阅的内容,并获得了如下数据:“您介意在问题中编辑最新版本的代码吗?我很好奇,您做了哪些更改以消除该错误?哦,对不起,我编辑了原始代码以显示我所做的操作。基本上,我使用了这个回复的响应,然后将x.src更改为x.get_attribute(“src”),因为这在python中不起作用。嗨,我今天尝试了这个,它说WebElement对象没有属性src。我不确定这是否有帮助,但我打印了xpath更改后正在审阅的内容,并获得了如下数据:“您介意在问题中编辑最新版本的代码吗?我很好奇,您做了哪些更改以消除该错误?哦,对不起,我编辑了原始代码以显示我所做的操作。基本上,我使用了这个回复的响应,然后将x.src更改为x.get_属性(“src”),因为它在python中不起作用