我在试着抓取http://quotes.toscrape.com/。它在一个页面上包含多个框,每个框包含一个引用、提供引用的人的姓名和该引用的标签。下面是我使用python在selenium webdriver中所做的工作:
driver = webdriver.Chrome()
driver.get("http://quotes.toscrape.com/")
sleep(2)
all_boxes = driver.find_elements_by_xpath(r"//div[@class='quote']")
for each in all_boxes:
print(each.find_element_by_xpath('//span').text) // to print the quote我在这里所做的事情非常容易理解。我已经选择了该页面上的所有框,然后迭代每个框,尝试使用HTML结构中观察到的所需xpath打印每个框中包含的引号。但得到的输出并不是预期的。即使我在遍历每个框,每次输出也只打印第一个框中包含的引号。
输出为:
“The world as we have created it is a process of our thinking.It cannot be changed without changing our thinking.”
“The world as we have created it is a process of our thinking.It cannot be changed without changing our thinking.”
“The world as we have created it is a process of our thinking.It cannot be changed without changing our thinking.”
“The world as we have created it is a process of our thinking.It cannot be changed without changing our thinking.”
“The world as we have created it is a process of our thinking.It cannot be changed without changing our thinking.”
“The world as we have created it is a process of our thinking.It cannot be changed without changing our thinking.”
“The world as we have created it is a process of our thinking.It cannot be changed without changing our thinking.”
“The world as we have created it is a process of our thinking.It cannot be changed without changing our thinking.”
“The world as we have created it is a process of our thinking.It cannot be changed without changing our thinking.”
“The world as we have created it is a process of our thinking.It cannot be changed without changing our thinking.”在这种非常具体的方法中,我找不到哪里出了问题。请只告诉我在这种方法中出了什么问题,因为我非常了解使用selenium或python的漂亮汤库进行抓取的其他技术。我只想知道为什么上面的编码方法不起作用。
发布于 2018-05-18 23:37:08
要抓取网站http://quotes.toscrape.com/并提取报价,您必须构建一个定位策略,该策略将识别网页上的所有报价,然后诱导WebDriverWait使所有元素可见,并将它们存储在List中。最后,您可以使用text方法提取遵循以下解决方案的所有文本:
从executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')导入webdriver从selenium.webdriver.chrome.options导入选项从selenium.webdriver.common.by导入方式从selenium.webdriver.support.ui导入WebDriverWait从selenium.webdriver.support导入expected_conditions as EC options = Options() options.add_argument("start-maximized") options.add_argument("disable-infobars") options.add_argument("--disable-extensions") driver = webdriver.Chrome(chrome_options=options,selenium driver.get("http://quotes.toscrape.com/") all_boxes = WebDriverWait(driver,20).until(EC.visibility_of_all_elements_located((By.XPATH,“//div@class=‘quote’/span@class=‘text’”)对于all_boxes中的每个: print(each.text)
“我们创造的世界是我们思考的过程。如果不改变我们的思维,它是无法改变的。““是我们的选择,哈利,比我们的能力更能显示我们的真实面貌。”“生活只有两种方式。一种是,好像没有什么是奇迹。另一种是,好像一切都是奇迹。“对一本好小说不感兴趣的人,不管是先生还是女士,一定愚蠢到无法忍受的地步。“不完美就是美,疯狂就是天才,绝对荒谬总比绝对无聊要好。”“不要成为一个成功的人。而是成为一个有价值的人。““因为你是什么而被人恨,总比因为你不是什么而被人爱要好。”“我没有失败。我刚刚找到了一万种行不通的方法。““女人就像一个茶包,只有放在热水里,你才知道它有多硬。”“没有阳光的一天就像是夜晚。”说。
发布于 2019-11-26 22:22:20
是你的xpath在迭代中出错了。您应该给出的是当前正在迭代的元素的相对路径,而不是整个文档。因此,不是
each.find_element_by_xpath('//span').text把这个放在
each.find_element_by_xpath('./span').texthttps://stackoverflow.com/questions/50399540
复制相似问题