how to extract data from autocomplete box with selenium python
我正在尝试从搜索框中提取数据,您可以在Wikipedia上看到一个很好的例子
这是我的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | driver = webdriver.Firefox() driver.get(response.url) city = driver.find_element_by_id('searchInput') city.click() city.clear() city.send_keys('a') time.sleep(1.5) #waiting for ajax to load selen_html = driver.page_source #print selen_html.encode('utf-8') hxs = HtmlXPathSelector(text=selen_html) ajaxWikiList = hxs.select('//div[@class="suggestions"]') items=[] for city in ajaxWikiList: item=TestItem() item['ajax'] = city.select('/div[@class="suggestions-results"]/a/@title').extract() items.append(item) print items |
Xpath表达式还可以,我在静态页面上进行了检查。如果我取消注释打印出废弃html代码的行,则该框的代码将显示在文件末尾。但是由于某种原因,我无法使用上面的代码从中提取数据?我必须错过一些东西,因为我尝试了2种不同的来源,维基百科页面只是我无法提取这些数据的另一种来源。
有什么建议吗?谢谢!
代替传递
1 2 3 4 5 6 | selen_html = driver.find_element_by_class_name('suggestions').get_attribute('innerHTML') hxs = HtmlXPathSelector(text=selen_html) suggestions = hxs.select('//div[@class="suggestions-results"]/a/@title').extract() for suggestion in suggestions: print suggestion |
输出:
1 2 3 4 5 6 7 8 9 10 | Animal Association football Arthropod Australia AllMusic African American (U.S. Census) Album Angiosperms Actor American football |
请注意,最好使用selenium
- 我如何才能让Selenium Web驱动程序等待元素的访问,而不仅仅是出现?
- seleniumwaitForElement
此外,请注意,