我怎么可以登录到使用python一个网站吗? [英] How can I log into a website using python?
问题描述
我已经看到了这个其他问题:<一href=\"http://stackoverflow.com/questions/189555/how-to-use-python-to-login-to-a-webpage-and-retrieve-cookies-for-later-usage\">How使用Python登录到一个网页,检索饼干供以后使用?
I've seen this other question: How to use Python to login to a webpage and retrieve cookies for later usage?
然而,答案的一个简单的修改并没有为我工作,所以我不知道我怎么能实现我的目标。
However, a straightforward modification of that answer did not work for me, so I'm wondering how I can achieve my goal.
要放弃背景下,我试图登录到 https://mog.com/hp/sign_in
,然后从以下页面提取我的播放列表的名称: http://mog.com/my_mog/playlists
To give context, I'm trying to log into https://mog.com/hp/sign_in
and then extract the names of my playlists from the following page: http://mog.com/my_mog/playlists
我想这应该是pretty简单的人谁知道他们在做什么。一些基本的code登录到该网站,访问受密码保护的网页将是巨大的,它甚至会更好,如果你能在一两句话解释了code每一行是干什么的,所以我能更好地理解什么样的code在做什么。
I think this should be pretty straightforward for someone who knows what they are doing. Some basic code to log into the site and access a password-protected page would be great, and it would be even better if you could explain in a sentence or two what each line in the code is doing, so I can get a better understanding of what the code is doing.
推荐答案
试着用机械化:
import mechanize
br=mechanize.Browser()
br.open('https://mog.com/hp/sign_in')
br.select_form(nr=0)
br['user[login]']= your_login
br['user[password]']= your_password
br.submit()
br.retrieve('http://mog.com/my_mog/playlists','playlist.html')
编辑:结果
让你的链接,你可以补充一点:
to get your links you can add this:
for link in br.links():
print link.url, link.text
或从 playlist.html
开始,你可以使用的 Beautifulsoup 和正则表达式:
or, starting from playlist.html
, you could use Beautifulsoup and regex:
from BeautifulSoup import BeautifulSoup
import re
soup = BeautifulSoup(file('playlist.html').read())
for link in soup.findAll('a', attrs={'href': re.compile("your matching re")}):
print link.get('href')
这篇关于我怎么可以登录到使用python一个网站吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!