使用 urllib 登录网站 [英] Login on a site using urllib
本文介绍了使用 urllib 登录网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从该站点获取信息 http://cheese.formice.com/maps/@5865339 ,但是当我使用 urllib.urlopen 请求时,它说我需要登录,我使用的是以下代码:
I'm trying to get information from this site http://cheese.formice.com/maps/@5865339 , but when i request using urllib.urlopen, its says that i need to login, i was using this code:
import urllib
data = {
'login':'Cfmaccount',
'password':'tfmdev321',
'submit':'Login',
}
url = 'http://cheese.formice.com/login'
data = urllib.urlencode(data)
response = urllib.urlopen(url, data)
我做错了什么?
推荐答案
它没有直接使用 urllib
,但您可能会发现使用 requests
包.requests
有一个 session
对象 查看此答案
It's not using urllib
directly, but you may find it easier working with the requests
package. requests
has a session
object see this answer
import requests
url = 'http://cheese.formice.com/forum/login/login'
login_data = dict(login='Cfmaccount', password='tfmdev321')
session = requests.session()
r = session.post(url, data=login_data)
这将使您登录到该站点.您可以通过以下方式验证:
That will log you in to the site. You can verify with:
print r.text #prints the <html> response.
登录后,您可以调用您想要的特定网址.
Once logged in, you can call the specific url you want.
r2 = session.get('http://cheese.formice.com/maps/@5865339')
print r2.content #prints the raw html you can now parse and scrape
这篇关于使用 urllib 登录网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文