溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python爬蟲為什么會獲取知乎內(nèi)容失敗

發(fā)布時間:2020-11-21 13:36:29 來源:億速云 閱讀:292 作者:小新 欄目:編程語言

這篇文章主要介紹python爬蟲為什么會獲取知乎內(nèi)容失敗,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

問題:已經(jīng)進(jìn)行模擬登入后,在獲取首頁信息時還是獲取到了注冊登入頁面的,是根本沒有登入上還是什么情況?

 

解決:

關(guān)于取不到內(nèi)容的原因,應(yīng)該就是登錄需要驗證碼的問題。

代碼:

_Zhihu_URL = 'http://www.zhihu.com'
_Login_URL = _Zhihu_URL + '/login'
_Captcha_URL_Prefix = _Zhihu_URL + '/captcha.gif?r='
_Cookies_File_Name = 'cookies.json'
 
_session = None
_header = {'X-Requested-With': 'XMLHttpRequest',
           'Referer': 'http://www.zhihu.com',
           'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64; '
                         'Trident/7.0; Touch; LCJB; rv:11.0)'
                         ' like Gecko',
           'Host': 'www.zhihu.com'}
 
def get_captcha_url():
    """獲取驗證碼網(wǎng)址
 
    :return: 驗證碼網(wǎng)址
    :rtype: str
    """
    return _Captcha_URL_Prefix + str(int(time.time() * 1000))
 
def _save_captcha(url):
    global _session
    r = _session.get(url)
    with open('code.gif', 'wb') as f:
        f.write(r.content)
 
def login(email='', password='', captcha='', savecookies=True):
    """不使用cookies.json,手動登陸知乎
 
    :param str email: 郵箱
    :param str password: 密碼
    :param str captcha: 驗證碼
    :param bool savecookies: 是否要儲存cookies文件
    :return: 一個二元素元祖 , 第一個元素代表是否成功(0表示成功),
        如果未成功則第二個元素表示失敗原因
    :rtype: (int, dict)
    """
    global _session
    global _header
    data = {'email': email, 'password': password,
            'rememberme': 'y', 'captcha': captcha}
    r = _session.post(_Login_URL, data=data)
    j = r.json()
    c = int(j['r'])
    m = j['msg']
    if c == 0 and savecookies is True:
        with open(_Cookies_File_Name, 'w') as f:
            json.dump(_session.cookies.get_dict(), f)
    return c, m
 
def create_cookies():
    """創(chuàng)建cookies文件, 請跟隨提示操作
 
    :return: None
    :rtype: None
    """
    if os.path.isfile(_Cookies_File_Name) is False:
        email = input('email: ')
        password = input('password: ')
        url = get_captcha_url()
        _save_captcha(url)
        print('please check code.gif for captcha')
        captcha = input('captcha: ')
        code, msg = login(email, password, captcha)
 
        if code == 0:
            print('cookies file created!')
        else:
            print(msg)
        os.remove('code.gif')
    else:
        print('Please delete [' + _Cookies_File_Name + '] first.')
 
def _init():
    global _session
    if _session is None:
        _session = requests.session()
        _session.headers.update(_header)
        if os.path.isfile(_Cookies_File_Name):
            with open(_Cookies_File_Name, 'r') as f:
                cookies_dict = json.load(f)
                _session.cookies.update(cookies_dict)
        else:
            print('no cookies file, this may make something wrong.')
            print('if you will run create_cookies or login next, '
                  'please ignore me.')
            _session.post(_Login_URL, data={})
    else:
        raise Exception('call init func two times')
 
_init()

以上是python爬蟲為什么會獲取知乎內(nèi)容失敗的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI