selenium与python web爬虫

selenium与python web爬虫,python,selenium,web-crawler,Python,Selenium,Web Crawler,我想屏幕刮有多个网页的网站。这些页面在不更改URL的情况下动态加载。因此,我使用硒来筛选和刮除它。但是这个简单的程序有一个例外 import re from contextlib import closing from selenium.webdriver import Firefox url="http://www.samsung.com/in/consumer/mobile-phone/mobile-phone/smartphone/" with closing(Firefox())

我想屏幕刮有多个网页的网站。这些页面在不更改URL的情况下动态加载。因此,我使用硒来筛选和刮除它。但是这个简单的程序有一个例外

import re
from contextlib import closing
from selenium.webdriver import Firefox 

url="http://www.samsung.com/in/consumer/mobile-phone/mobile-phone/smartphone/"

with closing(Firefox()) as browser:
    n = 2
    link = browser.find_element_by_link_text(str(n))
    link.click()
    #web_page=browser.page_source
    #print type(web_page)
错误如下

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: u'Unable to locate element: {"method":"link text","selector":"2"}' ; Stacktrace: Method FirefoxDriver.prototype.findElementInternal_ threw an error in file:///tmp/tmpMJeeTr/extensions/fxdriver@googlecode.com/components/driver_component.js 
这是给定url的问题还是firefox浏览器的问题。
如果有人帮了我,那会很有帮助。

我想你的主要问题是页面本身需要一段时间才能加载,你会立即尝试访问该链接(该链接可能尚未呈现,因此堆栈跟踪)。您可以尝试的一件事是在
浏览器中使用隐式等待,这将告诉
浏览器在超时之前等待元素出现的特定时间段。在您的情况下,您可以尝试以下操作,在为特定项轮询DOM时,最多需要等待10秒(在本例中,是链接文本
2
):


我正在开发一个python模块,它可能涵盖您(或其他人)的用例:

它将记录的selenium脚本转换为爬网函数,从而避免编写上述任何代码。它适用于动态加载内容的页面。我希望有人觉得这有用

browser.implicitly_wait(10)
n = 2
link = browser.find_element_by_link_text(str(n))
link.click()
#web_page=browser.page_source
#print type(web_page)