Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.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
通过Python中的web抓取工具登录到网站_Python_Selenium_Beautifulsoup_Urllib2_Web Scraping - Fatal编程技术网

通过Python中的web抓取工具登录到网站

通过Python中的web抓取工具登录到网站,python,selenium,beautifulsoup,urllib2,web-scraping,Python,Selenium,Beautifulsoup,Urllib2,Web Scraping,我正在使用Python中的SeleniumWebDriver进行web抓取项目 我想通过输入登录详细信息,然后单击提交按钮登录 我可以输入用户名和密码。但我无法用鼠标单击提交按钮 “提交”按钮的类型为 <input type="image" src="/images/buttons/loginnow.gif" tabindex="3"> 我得到以下错误: AttributeError:“WebDriver”对象没有“单击”属性 任何关于如何修复它或使用Python登录网站的任何其他

我正在使用Python中的SeleniumWebDriver进行web抓取项目

我想通过输入登录详细信息,然后单击提交按钮登录

我可以输入用户名和密码。但我无法用鼠标单击提交按钮

“提交”按钮的类型为

<input type="image" src="/images/buttons/loginnow.gif" tabindex="3">
我得到以下错误:

AttributeError:“WebDriver”对象没有“单击”属性

任何关于如何修复它或使用Python登录网站的任何其他替代解决方案的想法


谢谢

我很幸运地使用了
mechanize
。它非常简单明了

下面是我制作的一个脚本的精简版本:

from BeautifulSoup import BeautifulSoup
from tidylib import tidy_document

import mechanize
import cookielib

if __name__ == '__main__':
  browser = mechanize.Browser()

  cookiejar = cookielib.LWPCookieJar()
  browser.set_cookiejar(cookiejar)

  browser.set_handle_equiv(True)
  browser.set_handle_redirect(True)
  browser.set_handle_referer(True)
  browser.set_handle_robots(False)

  browser.open('https://www.example.com/')

  browser.select_form(name = 'loginform')
  browser['username'] = 'foo'
  browser['password'] = 'bar'

  browser.submit()

  browser.open(browser.click_link(text = 'Link text'))

  soup = BeautifulSoup(tidy_document(browser.response().read())[0])
你不需要点击图片,真的。您只需填写所有适当的表单详细信息,然后
submit()
it即可


另外,如果您不想解析任何内容,只需去掉
BeautifulSoup
tidylib
依赖项。

您需要调用元素的click函数,而不是驱动程序

submitButton=driver.find_element_by_xpath("//input[@type='image'][@src='/images/buttons/loginnow.gif']")
submitButton.click()

誓言这真是太神奇了,事实证明,Selenium还有
submit()
,它对我有效,甚至比重写脚本更好:P
submitButton=driver.find_element_by_xpath("//input[@type='image'][@src='/images/buttons/loginnow.gif']")
submitButton.click()