我看过另一个问题:How to use Python to login to a webpage and retrieve cookies for later usage?
但是,直接修改该答案对我不起作用,所以我想知道如何实现我的目标。
为了提供上下文,我正在尝试登录 https://mog.com/hp/sign_in
,然后从以下页面提取我的播放列表的名称:http://mog.com/my_mog/playlists
我认为对于知道自己在做什么的人来说,这应该是非常简单的。一些登录网站和访问受密码保护页面的基本代码会很棒,如果你能用一两句话解释代码中每一行的作用,那就更好了,这样我就能更好地理解代码正在做什么。
最佳答案
试试mechanize :
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')
编辑:
要获取您的链接,您可以添加以下内容:
for link in br.links():
print link.url, link.text
或者,从 playlist.html
开始,您可以使用 Beautifulsoup和正则表达式:
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 - 我如何使用python登录网站?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4414683/