Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
651 views
in Technique[技术] by (71.8m points)

authentication - How can I log into a website using 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.

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try with 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')

EDIT:
to get your links you can add this:

for link in br.links():
    print link.url, link.text

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')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...