如何使用Python在需要谷歌账号登录的网站上进行网络爬虫?

obj*_*gic 2 python web-crawler

我是 Python 新手,我想用 Python 抓取几个需要 Google 帐户登录的网站。例如,我想抓取一个网站 xxx.appspot.com,我需要登录我的 Google 帐户才能访问它,因为这数据库需要验证,我是授权人员。

当我做这样的事情时:

content=urllib.urlopen(target_url).read()
Run Code Online (Sandbox Code Playgroud)

我得到的“内容”当然只是一个登录页面。我如何实现代码以便抓取工具可以在我进行实际抓取之前登录 Google 帐户。

pra*_*890 5

尝试使用mechanizecookielib

下面的代码对我来说适用于 gmail 登录。用你各自的网址和东西试试这个。

import mechanize        

def gmaillogin():
    browser = mechanize.Browser(factory=mechanize.RobustFactory())
    browser.set_handle_robots(False)
    r = browser.open("https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=http://mail.google.com/mail/&scc=1&ltmpl=default&ltmplcache=2&emr=1") 
    browser.select_form(nr=0)               
    browser.form["Email"] = "emailid"
    browser.form["Passwd"] = "password"    
    browser.submit()                        

    html = browser.response().readlines()    
    
    print(html)  

                      
     

if __name__ == "__main__":
    gmaillogin()
Run Code Online (Sandbox Code Playgroud)