gpt4 book ai didi

Python urllib,urllib2 填表

转载 作者:太空宇宙 更新时间:2023-11-04 13:17:49 27 4
gpt4 key购买 nike

我想用 urllib2urllib 填写 HTML 表单。

import urllib
import urllib2

url = 'site.com/registration.php'
values = {'password' : 'password',
'username': 'username'
}

data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()

但是在表单的末尾是一个按钮(input type='submit')。如果您不单击该按钮,则无法发送您在 input(type text)

中写入的数据

如何使用 urlliburllib2 点击按钮?

最佳答案

如果您查看您的表单操作属性,您可以找出您的表单提交到的 uri。然后您可以向该 uri 发出 post 请求。这可以像这样完成:

import urllib
import urllib2

url = 'http://www.someserver.com/cgi-bin/register.cgi'
values = {'name' : 'Michael Foord',
'location' : 'Northampton',
'language' : 'Python' }

data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()

https://docs.python.org/2/howto/urllib2.html

您也可以使用 requests 库,这样会容易很多。你可以阅读它here

您可能需要考虑的另一件事是嵌入在表单中的 CSRF(跨站请求伪造) token 。您将需要了解如何获取它并将其与您的帖子一起传递。

关于Python urllib,urllib2 填表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22869977/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com