Login on a site using urllib

前端 未结 2 1431
春和景丽
春和景丽 2020-12-09 23:58

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 t

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-10 00:36

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

提交回复
热议问题