【发布时间】:2018-11-07 13:10:40
【问题描述】:
我正在尝试收集一些亚马逊问题和答案,特别是这个: https://www.amazon.com/ask/questions/Tx1AYFFVMESHMZV/ref=ask_ql_ql_al_hza
这是每个问题的 html 部分(可以在开发人员工具中进行 html 检查以获取更多详细信息):
<span class="askExpanderContainer noScriptNotDisplayExpander">
<span class="askShortText">
They definitely help stretch the toes. I'm hoping to avoid a hammer toe that has been developing on one foot, and I'm not sure they're doing that, but I read that one way to avoid hammer toes developing is to stretch the toes, so I figure they will help in the long run and probably won't do any harm. From the beginning…
<a class="a-link-normal askSeeMore" href="#">
see more
</a>
</span>
<span class="askLongText">
They definitely help stretch the toes. I'm hoping to avoid a hammer toe that has been developing on one foot, and I'm not sure they're doing that, but I read that one way to avoid hammer toes developing is to stretch the toes, so I figure they will help in the long run and probably won't do any harm. From the beginning never hurt my toes, so I didn't have an adjustment period like some people have described. I use them every day, and I enjoy being calm and still for half an hour or 45 minutes while all I do is stretch my toes. That's worth something too!
<a class="a-link-normal askSeeLess" href="#">
see less
</a>
</span>
</span>
我需要完整的答案,但是当我尝试查找 askLongText 元素时,我收到以下错误:
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such
element: Unable to locate element: {"method":"css selector","selector":"span.askLongText"}
(Session info: chrome=66.0.3359.181)
(Driver info: chromedriver=2.38.552518 (183d19265345f54ce39cbb94cf81ba5f15905011),platform=Mac OS X 10.13.4 x86_64)
但是,我可以成功提取 askShortText 元素。
这是python代码:
driver.get(url)
title = driver.find_element_by_css_selector('p.a-size-large.askAnswersAndComments.askWrapText').text
answers_section = driver.find_element_by_css_selector('div.a-section.askAnswersAndComments.askWrapText')
answers = answers_section.find_elements_by_xpath('div[@id]')
for ans in answers:
answer = ans.find_element_by_css_selector('span.askLongText').text
print answer
注意:最后三个答案元素不包含 askLongText 类。稍后我将处理异常,但没有将其放在这里用于测试目的。无论哪种方式,前三个元素确实包含此类,并且应该打印其内容,但这不会发生
【问题讨论】:
-
你真的知道如何选择答案@Ian Spitz 吗?看来您正在等待更多的答案出现。不是吗?
标签: python selenium web-scraping amazon