简体   繁体   English

Python登录网页问题

[英]Python Login to Webpage Issue

I am using Spyder to code my project in Python 3.6. 我正在使用Spyder在Python 3.6中编码我的项目。

I've downloaded robobrowser to assist me in logging into a webpage since I am new to web-scraping and html. 由于我不熟悉网络抓取和html,因此我下载了robobrowser来帮助我登录到网页。

I understand the process, but I'm having an issue when accessing form elements. 我了解该过程,但是在访问表单元素时遇到问题。

from robobrowser import RoboBrowser

browser = RoboBrowser(history=True)
url1 = "example_url"

username = "example_user"
password = "example_pass"

browser.open(url1)

form = browser.get_forms()
form['username'] = username

There appears to be only one form and when I tried an example converting it to a string, it appeared to be finding the form. 似乎只有一种形式,当我尝试将其转换为字符串的示例时,似乎正在寻找该形式。 For some reason it doesn't show up as a variable in the spyder workspace unless i convert to a string. 由于某种原因,除非我转换为字符串,否则它不会在spyder工作区中显示为变量。

I looked at the html and the name as well as the id for the username entry is indeed 'username'. 我查看了html,用户名条目的名称和ID的确是'username'。 I get the error 我得到错误

form['username'] = username
       TypeError: list indices must be integers or slices, not str

It doesn't like me trying to access the index in form by 'username', however this is the syntax in all of the examples for this module that i've seen. 它不喜欢我尝试按“用户名”形式访问索引,但是这是我所看到的该模块所有示例中的语法。 (First answer to this link is what I'm using as a template https://www.quora.com/What-is-the-best-way-to-log-in-to-a-website-using-Python ) (对此链接的第一个答案是我正在使用的模板https://www.quora.com/What-is-the-best-way-to-log-in-to-a-website-using-Python

You may try using login and password directly in the url. 您可以尝试直接在网址中使用登录名和密码。 Here is the scheme: 这是方案:

http://USERNAME:PASSWORD@example.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM