1. 程式人生 > >python3爬取部落格瀏覽量

python3爬取部落格瀏覽量

爬取結果

這裡寫圖片描述


程式碼很簡單:

# encoding=utf8
import requests
import re
import time
from bs4 import BeautifulSoup

firstUrl = 'http://blog.csdn.net/snake_son/article/details/52282490'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
    ,
    'Cookie': '_message_m=23yegwleahbzf4fy5a05grgr; uuid=e7680a5d-2824-45d9-ac7a-06289c3d3cd8; avh=53945000%2c52282490; dc_tos=os5x0v; dc_session_id=1498493448566'
}

def getHtml(url):
    text = requests.get(url,headers).text
    # print('text  ',text)
    return text

# txt = getHtml(firstUrl)

def parseHtml(text):
    reg_next = r'blog_articles_xiayipian.*?location.href=(.*?);">'
    regNext = re.compile(reg_next)
    nextUrl = re.findall(regNext,text)
    print('當前新頁面: ',nextUrl)
    str1 = ''.join(nextUrl).rstrip('\'')
    # htmurl = 'http://blog.csdn.net'+''.join(str1).rstrip('\'')

    # 將字串前n個字元替換為指定的字元
    # strnset(sStr1,ch,n)
    sStr1 = ''.join(str1)
    ch = ''
    n = 1
    sStr1 = n * ch + sStr1[1:]
    htmurl = 'http://blog.csdn.net'+sStr1
    print('htmurl  '+htmurl)
    return htmurl


for i in range(1,56):
    text = getHtml(firstUrl)
    newUrl = parseHtml(text)
    firstUrl = newUrl
    print('first2 ',firstUrl,'newUrl ',newUrl)

依然用的是re,覺得用的還是蠻不錯的,一開始學習java 是很拒絕的,因為這個玩意那時候學的一臉懵逼,現在可倒好,很多教程說python3 爬取內容,用beautifulsoup,xpath,能不用re的話,就儘量不用,雖然我用過beautifulsoup,但是現在估計也有些陌生了,現在用的正則確實蠻爽的,越用越熟練。這次爬取刷部落格瀏覽量是為了驗證能

不能真的是進行瀏覽量的增加,

答案是可以的

進行瀏覽量的刷,我是獲取最早的一篇部落格,然後選擇下一頁按鈕進行重新new 一個url再次訪問的。其他就是可能在list 與string方面稍微遇到點問題,進行url部分的去除操

作,有必要的說一點的是,類似python這種後端開發,基礎還是很重要的,更多的是自己動手來解決問題的能力,學會找錯,排bug,這次爬取很簡單,半小時沒用到,主要是鍛鍊回來晚了,

寫到最後: 實現了自己以前對剛開始部落格想讓著瀏覽量劇增的想法,想著有多少人看過我寫過的部落格,現在實現了想想,確實簡單,自己的瀏覽量也就昨晚爬過兩次,進行校驗,so。 每次小小的成就都會讓你越來越喜歡上它的

--------------------- 作者:徐代龍 來源:CSDN 原文:https://blog.csdn.net/snake_son/article/details/73825007?utm_source=copy 版權宣告:本文為博主原創文章,轉載請附上博文連結!