Python爬蟲入門(一)獲取源碼

舉個例子,爬一爬知乎日報的相關數據 http://daily.zhihu.com/

1、獲取源碼

import requests

url = 'http://daily.zhihu.com/'
res = requests.get(url).text
print(res)

個人喜歡requests,直接訪問,發現返回500錯誤

C:\Python35\python.exe F:/PyCharm/爬蟲/daily.py
<html><body><h1>500 Server Error</h1>
An internal server error occured.
</body></html>


Process finished with exit code 0

根據經驗判斷,是知乎禁止爬蟲,需要加上一些偽裝,讓我們看看加上瀏覽器偽裝效果

import requests

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
url = 'http://daily.zhihu.com/'
res = requests.get(url,headers=headers).text
print(res)

看看結果,已經返回我們需要的數據

C:\Python35\python.exe F:/PyCharm/爬蟲/daily.py
<!DOCTYPE html><html lang="zh-CN"><head><title>知乎日報 - 每天 3 次,每次 7 分鐘</title><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><meta name="description" content="在中國,資訊類移動應用的人均閱讀時長是 5 分鐘,而在知乎日報,這個數字是 21。以獨有的方式為你提供最高質、最深度、最有收獲的閱讀體驗。"><link rel="stylesheet" href="/css/base.auto.css"><link rel="stylesheet" href="/css/new_home_v3.auto.css"><script src="/js/jquery.1.9.1.js"></script><script src="/js/new_index_v3/home.js"></script><link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"><base target="_blank"><style>h1,h2,h3 {padding: 0;margin:0}</style><base target="_blank"></head><body class="home"><a href="javascript:;" title="回到頂部" class="back-to-top"></a><div class="header navbar-fixed-top"><div class="container-fixed-width clearfix"><div class="top-nav-link"><a href="javascript:;" data-offset="470"><span>瀏覽內容</span></a><a href="javascript:;" data-offset="0" class="active"><span>App 下載</span></a></div><h1 class="logo"><a  title="知乎日報" class="link-logo">知乎日報</a></h1></div></div><div class="download">
...

但是這種寫法是否可以應用到所有的網站,答案是“不”

2、代理設置

有時候同一個IP去爬取同一網站上的內容,久了之后就會被該網站服務器屏蔽。解決方法就是更換IP。這個時候,在對方網站上,顯示的不是我們真實地IP地址,而是代理服務器的IP地址。

http://www.xicidaili.com/nn/
西刺代理提供了很多可用的國內IP,可以直接拿來使用。

那么如何在爬蟲里加入代理呢,看看requests的官方文檔怎么說。http://docs.python-requests.org/zh_CN/latest/user/advanced.html#proxies

如果需要使用代理,你可以通過為任意請求方法提供 proxies 參數來配置單個請求:

import requests

proxies = {
  "http": "http://10.10.1.10:3128",
  "https": "http://10.10.1.10:1080",
}

requests.get("http://example.org", proxies=proxies)

用法很簡單,加入proxies參數即可
import requests

proxies = {
  "http": "http://121.201.24.248:8088",
  "https": "http://36.249.194.52:8118",
}
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
url = 'http://daily.zhihu.com/'
res = requests.get(url,headers=headers,proxies=proxies).text
print(len(res))

為了便于測試,只打印出返回數據的長度

C:\Python35\python.exe F:/PyCharm/爬蟲/daily.py
10830

Process finished with exit code 0

發現代理服務器成功爬取知乎日報的信息,內容是10830,故意把代理IP寫錯一位數,看看結果

import requests

proxies = {
  "http": "http://121.201.24.248:8088",
  "https": "http://36.249.194.52: 222",
}
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
url = 'http://daily.zhihu.com/'
res = requests.get(url,headers=headers,proxies=proxies).text
print(len(res))

我們把"https": "http://36.249.194.52:8118"修改為"https": "http://36.249.194.52: 222",此時返回的結果如下,發現不能獲取網頁數據。所以,在使用代理服務器爬去網站時,如果出現異常,要考慮代理IP是否失效了。
當然你也可以寫一個爬蟲,實時抓取最新的代理IP用來爬取。

Traceback (most recent call last):
  File "F:/PyCharm/爬蟲/daily.py", line 9, in <module>
    res = requests.get(url,headers=headers,proxies=proxies).text
  File "C:\Python35\lib\site-packages\requests\api.py", line 70, in get
    return request('get', url, params=params, **kwargs)
  File "C:\Python35\lib\site-packages\requests\api.py", line 56, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Python35\lib\site-packages\requests\sessions.py", line 488, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Python35\lib\site-packages\requests\sessions.py", line 609, in send
    r = adapter.send(request, **kwargs)
  File "C:\Python35\lib\site-packages\requests\adapters.py", line 485, in send
    raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPConnectionPool(host='121.201.24.248:8088', port=80): Max retries exceeded with url: http://daily.zhihu.com/ (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x0000000003860DA0>: Failed to establish a new connection: [Errno 11004] getaddrinfo failed',)))

3、模擬登錄

有些網站是需要登錄才能看到信息的,例如知乎,直接用requests獲取知乎首頁信息,返回數據是需要你登錄的,只有登錄了才能看到數據。

<button type="button" class="signin-switch-button">手機驗證碼登錄</button>

<a class="unable-login" href="#">無法登錄?</a>
</div>

<div class="social-signup-wrapper" data-za-module="SNSSignIn">
<span class="name js-toggle-sns-buttons">社交帳號登錄</span>

<div class="sns-buttons">
<a title="微信登錄" class="js-bindwechat" href="#"><i class="sprite-index-icon-wechat"></i></a>
<a title="微博登錄" class="js-bindweibo" href="#"><i class="sprite-index-icon-weibo"></i></a>
<a title="QQ 登錄" class="js-bindqq" href="#"><i class="sprite-index-icon-qq"></i></a>
</div>

再次回到官方文檔http://docs.python-requests.org/zh_CN/latest/user/quickstart.html#cookie

如果某個響應中包含一些 cookie,你可以快速訪問它們:

>>> url = 'http://example.com/some/cookie/setting/url'
>>> r = requests.get(url)

>>> r.cookies['example_cookie_name']
'example_cookie_value'
要想發送你的cookies到服務器,可以使用 cookies 參數:

>>> url = 'http://httpbin.org/cookies'
>>> cookies = dict(cookies_are='working')

>>> r = requests.get(url, cookies=cookies)
>>> r.text
'{"cookies": {"cookies_are": "working"}}'

具體的分析過程可以參考xchaoinfo所寫的文章和視頻,講解十分清晰
https://zhuanlan.zhihu.com/p/25633789
下面是代碼

import requests
from bs4 import BeautifulSoup
import os, time
import re
# import http.cookiejar as cookielib

# 構造 Request headers
agent = 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Mobile Safari/537.36'
headers = {
    "Host": "www.zhihu.com",
    "Referer": "https://www.zhihu.com/",
    'User-Agent': agent
}

######### 構造用于網絡請求的session
session = requests.Session()
# session.cookies = cookielib.LWPCookieJar(filename='zhihucookie')
# try:
#     session.cookies.load(ignore_discard=True)
# except:
#     print('cookie 文件未能加載')

############ 獲取xsrf_token
homeurl = 'https://www.zhihu.com'
homeresponse = session.get(url=homeurl, headers=headers)
homesoup = BeautifulSoup(homeresponse.text, 'html.parser')
xsrfinput = homesoup.find('input', {'name': '_xsrf'})
xsrf_token = xsrfinput['value']
print("獲取到的xsrf_token為: ", xsrf_token)

########## 獲取驗證碼文件
randomtime = str(int(time.time() * 1000))
captchaurl = 'https://www.zhihu.com/captcha.gif?r='+\
             randomtime+"&type=login"
captcharesponse = session.get(url=captchaurl, headers=headers)
with open('checkcode.gif', 'wb') as f:
    f.write(captcharesponse.content)
    f.close()
# os.startfile('checkcode.gif')
captcha = input('請輸入驗證碼:')
print(captcha)

########### 開始登陸
headers['X-Xsrftoken'] = xsrf_token
headers['X-Requested-With'] = 'XMLHttpRequest'
loginurl = 'https://www.zhihu.com/login/email'
postdata = {
    '_xsrf': xsrf_token,
    'email': '郵箱@qq.com',
    'password': '密碼'
}
loginresponse = session.post(url=loginurl, headers=headers, data=postdata)
print('服務器端返回響應碼:', loginresponse.status_code)
print(loginresponse.json())
# 驗證碼問題輸入導致失敗: 猜測這個問題是由于session中對于驗證碼的請求過期導致
if loginresponse.json()['r']==1:
    # 重新輸入驗證碼,再次運行代碼則正常。也就是說可以再第一次不輸入驗證碼,或者輸入一個錯誤的驗證碼,只有第二次才是有效的
    randomtime = str(int(time.time() * 1000))
    captchaurl = 'https://www.zhihu.com/captcha.gif?r=' + \
                 randomtime + "&type=login"
    captcharesponse = session.get(url=captchaurl, headers=headers)
    with open('checkcode.gif', 'wb') as f:
        f.write(captcharesponse.content)
        f.close()
    os.startfile('checkcode.gif')
    captcha = input('請輸入驗證碼:')
    print(captcha)

    postdata['captcha'] = captcha
    loginresponse = session.post(url=loginurl, headers=headers, data=postdata)
    print('服務器端返回響應碼:', loginresponse.status_code)
    print(loginresponse.json())




##########################保存登陸后的cookie信息
# session.cookies.save()
############################判斷是否登錄成功
profileurl = 'https://www.zhihu.com/settings/profile'
profileresponse = session.get(url=profileurl, headers=headers)
print('profile頁面響應碼:', profileresponse.status_code)
profilesoup = BeautifulSoup(profileresponse.text, 'html.parser')
div = profilesoup.find('div', {'id': 'rename-section'})
print(div)

好了關于爬蟲的第一步,獲取源碼這一節講了很多,其實大多數網站加上User-Agent和代理IP就可以正常爬取。下一節會講講如何利用xpath來解析網頁,獲取我們想要的數據。

喜歡爬蟲、數據的可以關注一下我的微信公眾號,多多交流。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Python學習網絡爬蟲主要分3個大的版塊:抓取,分析,存儲 另外,比較常用的爬蟲框架Scrapy,這里最后也詳細...
    楚江數據閱讀 1,482評論 0 6
  • Python入門網絡爬蟲之精華版 網址: https://github.com/lining0806/Python...
    ZHANG_GO閱讀 663評論 0 2
  • 基礎知識 HTTP協議 我們瀏覽網頁的瀏覽器和手機應用客戶端與服務器通信幾乎都是基于HTTP協議,而爬蟲可以看作是...
    腩啵兔子閱讀 1,512評論 0 17
  • 聲明:本文講解的實戰內容,均僅用于學習交流,請勿用于任何商業用途! 一、前言 強烈建議:請在電腦的陪同下,閱讀本文...
    Bruce_Szh閱讀 12,783評論 6 28
  • 找了份很無聊并且很累人的兼職——促銷華夫餅,工作很簡單,就是讓別人試吃,最終的目的還是在于把華夫賣出去。 多去看看...
    孫秋瞳瑾閱讀 177評論 0 0