爬蟲日記---小白自學(xué)知識點記錄(記七日熱點)

先開始說一段廢話吧。。。
我是從3月中旬開始接觸Python。當(dāng)時我想學(xué)習(xí)如何去做出比較專業(yè)的數(shù)據(jù)分析圖,然后我家人說R語言和Python都可以做出圖來。但他建議學(xué)Python。我在網(wǎng)上找學(xué)習(xí)的資料,偶爾之間看到python可以實現(xiàn)爬蟲功能。正如彭老師的簡書中寫到的“學(xué)習(xí)Python,比較好快速找到應(yīng)用的場景”??吹脚老x取得各個網(wǎng)頁大數(shù)據(jù)后做的數(shù)據(jù)分析,這也是我學(xué)習(xí)的興趣所在。學(xué)習(xí)過程饒了不少彎路,以下是我自己的一些教訓(xùn)吧。

python語言學(xué)習(xí)經(jīng)驗:

1、基礎(chǔ)知識要扎實,先要學(xué)習(xí)一些python的基本操作
參考一本《簡明 Python 教程》的文章,因為學(xué)過C語言,有些編程的基本常識,所以著重看了些:

      1)數(shù)據(jù)結(jié)構(gòu)(列表,元組,字典)
      2)控制流(if,for,try...except)
      3)函數(shù)(變量,參數(shù),實參,形參,參數(shù)的傳遞)
      4)類

這些在爬蟲中反復(fù)應(yīng)用到,一定要學(xué)習(xí)扎實,不要一開始就去找別人做的爬蟲案例。先把基礎(chǔ)知識點過一遍,至少知道python的語言結(jié)構(gòu)是怎么樣的。會打python代碼

2、了解爬蟲的原理。學(xué)習(xí)哪些關(guān)鍵知識點
我的經(jīng)驗是先了解html的網(wǎng)頁代碼,再搞清楚爬蟲的過程,然后一個一個知識點去查資料,去練習(xí)??梢栽诮K端先從幾行小代碼開始練習(xí)。最后在做一個小爬蟲,然后慢慢變大爬蟲,哈哈。。。。(我之前就是沒有搞清楚怎么入手學(xué)爬蟲,饒了一圈回到原點)

爬蟲的抓取過程:

先獲得網(wǎng)頁地址----〉解析網(wǎng)頁代碼----〉抓取和提取網(wǎng)頁內(nèi)容---〉存儲數(shù)據(jù)      是不是很簡單。。。。

小爬蟲變大爬蟲:

單頁爬蟲----〉多頁爬蟲----〉“迭代”爬蟲(就是有爬取鏈接頁面底下的頁面)----〉異步加載爬蟲---〉scrapy爬蟲(目前正在攻克中。。。)

爬蟲的小知識點主要在于抓取和提取網(wǎng)頁內(nèi)容:

1)BeatifulSoup

說到這個“美麗湯“,我真是又愛又恨。我第一次在終端用的時候就不能運行。大受打擊。原因是引入這個類的時候沒有區(qū)分大小
 錯誤:
from bs4 import beautifulsoup
正確:
from bs4 import BeautifulSoup
#bs4是一個python的庫,BeautifulSoup是這個庫里的一個類

知識點1:
soup.find_all('tag.name', class_='class_ value')
soup.find_all('tag.name',attrs ={'class':'class_ value'})

舉例:
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story"><a  class="sister" id="link1">Elsie</a>
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""
soup = BeautifulSoup(html_doc)
print soup.find_all('title')  #提取'title'標(biāo)簽的所有列表
print soup.find_all('p',class_='title') #提取含‘p'標(biāo)簽的且'class'屬性是'title'的所有列表
print soup.find_all('a')  #提取'a'標(biāo)簽的所有列表
print soup.find_all(id="link2") #提取id的'屬性是"link2"的所有列表

返回值為:
[<title>The Dormouse's story</title>]
[<p class="title"><b>The Dormouse's story</b></p>]
[<a class="sister"  id="link1">Elsie</a>, <a class="sister"  id="link2">Lacie</a>, <a class="sister"  id="link3">Tillie</a>]
[<a class="sister"  id="link2">Lacie</a>]

知識點2:
soup.select(tag.name1 tag.name2 tag.name3) #提取tag.name1標(biāo)簽下的tag.name2標(biāo)簽下的tag.name3的列表
soup.select('#****')
soup.select('.****')
soup.select('tag.name #***')
舉例:
soup.select('#sponsor') #通過id查找,搜索 id 為 sponsor 的標(biāo)簽列表
#可以在chrome網(wǎng)頁抓包中點擊你要抓的那個tag,看到整個tag的層次(見圖“抓包tag“)

soup.select('.ebox')   .這個點表示查詢class="ebox"的,所有標(biāo)簽內(nèi)容
soup.select('div #index_nav')  表示尋找div標(biāo)簽中id為index_nav的標(biāo)簽內(nèi)容。

為了方便查找自己抓取的內(nèi)容,以上可以把空格改為' 〉':
舉例:
soup.select("div > #index_nav")
抓包tag
  1. Xpath知識點
·在網(wǎng)頁源代碼中右鍵,Copy XPath

·// 定位根節(jié)點

·/ 往下層尋找

·提取文本內(nèi)容:/text()

·提取屬性內(nèi)容: /@xxxx

·以相同的字符開頭 starts-with(@屬性名稱, 屬性字符相同部分)

·標(biāo)簽套標(biāo)簽 string(.)

3)正則表達(dá)式
目前我還不是很會,所以不做描述

開始爬啦--爬取“簡書7日熱門“

我在這里就不做具體講解,可以看一下其他童鞋在爬蟲作業(yè)專題做的作業(yè),就我犯得一些錯誤和大家分享一下吧

#-*-coding:'utf-8'-*-
import requests
from lxml import etree
import csv
import re
import json
import sys
reload(sys)
sys.setdefaultencoding('utf8')
base_url='http://www.itdecent.cn/'

def information(url):

    html=requests.get(url).content
    selector=etree.HTML(html)
    datas=selector.xpath('//ul[@class="note-list"]/li')
    infos=[]
    for data in datas:
        detail_url=data.xpath('a/@href')
        if detail_url:
            detail_url1=detail_url[0]
            detail_url1='http://www.itdecent.cn'+detail_url1
            info=information2(detail_url1)
            infos.append(info)
    return infos

def information2(url):
    info=[]
    html=requests.get(url).content
    selector=etree.HTML(html)
    user=selector.xpath('//span[@class="name"]/a/text()')[0]
    title=selector.xpath('//div[@class="article"]/h1[@class="title"]/text()')[0]
    #read_qty = selector.xpath('//div[@class="meta"]/span[2]/text()')[0]
    #read_qty=re.findall('"views_count":(.*?),', html, re.S)[0]
    view_qty=re.findall('"views_count":(.*?),', html, re.S)[0]
    comment_qty=re.findall('"comments_count":(.*?),', html, re.S)[0]
    like_qty=re.findall('"likes_count":(.*?),', html, re.S)[0]
    id=re.findall('"id":(.*?),', html, re.S)[0]
    reward_url='http://www.itdecent.cn/notes/%s/rewards?count=20' %id
    html_reward=requests.get(reward_url).text
    reward_detail=json.loads(html_reward)
    reward_qty=reward_detail['rewards_count']
    html_collection_url='http://www.itdecent.cn/notes/%s/included_collections?page=1' %id
    collection=collections(html_collection_url,id)
    info.append(user)
    info.append(title)
    info.append(view_qty)
    info.append(comment_qty)
    info.append(like_qty)
    info.append(reward_qty)
    info.append(collection)
    return info

def collections(url,id):
    html=requests.get(url).content
    collection_detail=json.loads(html)
    pages=collection_detail['total_pages']
    collection_urls=['http://www.itdecent.cn/notes/{}/included_collections?page={}'.format(id,str(i)) for i in range(1,pages+1)]
    datas = []
    for url in collection_urls:
        html_collection=requests.get(url).content
        collection_detail=json.loads(html_collection)
        for one in collection_detail['collections']:
            datas.append(one['title'])
    data=','.join(datas)
    return(data)

if __name__=='__main__':
    urls=["http://www.itdecent.cn/trending/weekly?&page={}".format(i) for i in range(1,27)]
    csvfile=file('sevenday.csv','wb')
    writer=csv.writer(csvfile)
    #infos=[]
    for url in urls:
        resources=information(url)
        #infos.append(resoure)
        for resource in resources:
            writer.writerow(resource)
    csvfile.close()

每一個小程序都是在不斷的調(diào)試不斷的查資料中進(jìn)行的。
錯誤1:(印象也是最深刻的)
對xpath的抓數(shù)據(jù)理解不夠透徹,我想要抓取的是首頁的每個標(biāo)題的鏈接,看一下網(wǎng)頁代碼:

Paste_Image.png

分析:每一個href都存放在a標(biāo)簽下,a標(biāo)簽又存放在div.content下,思路:把所有l(wèi)i都提取到。然后for循環(huán)爬取li下的a下的href信息。(具體可以看圖片底部的標(biāo)簽name)。我們可以按照以下來做:

datas=selector.xpath('//ul[@class="note-list"]/li')
for data in datas:
   detail_url=data.xpath('a/@href')

我當(dāng)時寫成了:

datas=selector.xpath('//div[@class="list-container"]/ul[@class="note-list"]')
for data in datas:
    detail_url=data.xpath('li/a/@href')

經(jīng)過測試len[datas]結(jié)果是0,原因是其實提取了ul下滿足class="note-list"的標(biāo)簽。并沒有爬取到li這一層

錯誤2:提取分頁鏈接的url時候,發(fā)現(xiàn)程序一直出現(xiàn)報錯。
教訓(xùn):需要學(xué)會調(diào)試,從大的范圍開始調(diào)試,在一點縮小范圍,找出問題所在
print 所有的 detail_url時候發(fā)現(xiàn),有的url為空,故需要做一個判斷是否為空。

for data in datas:
   detail_url=data.xpath('a/@href')
   if detail_url:
       detail_url1=detail_url[0]

總結(jié)

謝謝程老師彭老師的指導(dǎo)幫助!
還有就是多看!多想!多問!最重要的是多做?。?!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 爬蟲文章 in 簡書程序員專題: like:128 - Python 爬取落網(wǎng)音樂 like:127 - 【圖文詳...
    treelake閱讀 29,744評論 33 638
  • 爬蟲文章 in 簡書程序員專題: like:128-Python 爬取落網(wǎng)音樂 like:127-【圖文詳解】py...
    喜歡吃栗子閱讀 22,683評論 4 411
  • # Python 資源大全中文版 我想很多程序員應(yīng)該記得 GitHub 上有一個 Awesome - XXX 系列...
    小邁克閱讀 3,122評論 1 3
  • 如何讓你遇見我? 在我最好的年歲。 為這—— 我在佛前虔誠發(fā)愿, 讓我們結(jié)一段善緣。 于是, 佛把我化做一棵菩提樹...
    岸隱閱讀 129評論 0 0
  • 看圖作詩,歡迎留言對擂,好詩送貝 (送貝多少,只代表打動我的程度,不代表詩的好差) 另外建議大家作詩前先點贊,并打...
    0d7be05d7933閱讀 4,580評論 86 157

友情鏈接更多精彩內(nèi)容