How can I log into a website using python?

后端 未结 1 1731
耶瑟儿~
耶瑟儿~ 2020-12-09 22:05

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

相关标签:
1条回答
  • 2020-12-09 22:38

    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')
    
    0 讨论(0)
提交回复
热议问题