Python 爬蟲實現(xiàn)增加播客訪問量的方法實現(xiàn)
一、序言:
世界 1024 程序猿節(jié)日不加班,閑著沒事兒。。。隨手寫了個播客訪問量爬蟲玩玩,訪問量過萬不是事兒?。?!每個步驟注釋都很清晰,代碼僅供學(xué)習(xí)參考!
---- Nick.Peng
二、所需環(huán)境:
Python3.x
相關(guān)模塊: requests、json、lxml、urllib、bs4、fake_useragent
三、增加Blog訪問量代碼如下:
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Author: Nick # @Date: 2019-10-24 15:40:58 # @Last Modified by: Nick # @Last Modified time: 2019-10-24 16:54:31 import random import re import time import urllib import requests from bs4 import BeautifulSoup from fake_useragent import UserAgent try: from lxml import etree except Exception as e: import lxml.html # 實例化一個etree對象(解決通過from lxml import etree導(dǎo)包失敗) etree = lxml.html.etree # 實例化UserAgent對象,用于產(chǎn)生隨機UserAgent ua = UserAgent() class BlogSpider(object): """ Increase the number of CSDN blog visits. """ def __init__(self): self.url = "https://blog.csdn.net/PY0312/article/list/{}" self.headers = { "Referer": "https://blog.csdn.net/PY0312/", "User-Agent": ua.random } self.firefoxHead = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0"} self.IPRegular = r"(([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5]).){3}([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])" def send_request(self, num): """ 模擬瀏覽器發(fā)起請求 :param num: num :return: html_str """ html_str = requests.get(self.url.format( num), headers=self.headers).content.decode() # print(html_str) return html_str def parse_data(self, html_str): """ 用于解析發(fā)起請求返回的數(shù)據(jù) :param html_str: :return: each_page_urls """ # 將返回的 html字符串 轉(zhuǎn)換為 element對象,用于xpath操作 element_obj = etree.HTML(html_str) # print(element_obj) # 獲取每一頁所有blog的url each_page_urls = element_obj.xpath( '//*[@id="mainBox"]/main/div[2]/div/h4/a/@href') # print(each_page_urls) return each_page_urls def parseIPList(self, url="http://www.xicidaili.com/"): """ 爬取最新代理ip,來源:西刺代理 注意:西刺代理容易被封,如遇到IP被封情況,采用以下兩種方法即可解決: 方法一:請參考我上一篇博客《Python 實現(xiàn)快代理IP爬蟲》 ===> 喜歡研究的同學(xué),可參考對接此接口 方法二:直接屏蔽掉此接口,不使用代理也能正常使用 :param url: "http://www.xicidaili.com/" :return: 代理IP列表ips """ ips = [] request = urllib.request.Request(url, headers=self.firefoxHead) response = urllib.request.urlopen(request) soup = BeautifulSoup(response, "lxml") tds = soup.find_all("td") for td in tds: string = str(td.string) if re.search(self.IPRegular, string): ips.append(string) # print(ips) return ips def main(self, total_page, loop_times, each_num): """ 調(diào)度方法 :param total_page: 設(shè)置博客總頁數(shù) :param loop_times: 設(shè)置循環(huán)次數(shù) :param each_num: 設(shè)置每一頁要隨機挑選文章數(shù) :return: """ i = 0 # 根據(jù)設(shè)置次數(shù),打開循環(huán) while i < loop_times: # 遍歷,得到每一頁的頁碼 for j in range(total_page): # 拼接每一頁的url,并模擬發(fā)送請求, 返回響應(yīng)數(shù)據(jù) html_str = self.send_request(j + 1) # 解析響應(yīng)數(shù)據(jù),得到每一頁所有博文的url each_page_urls = self.parse_data(html_str) # 調(diào)用parseIPList隨機產(chǎn)生代理IP,防反爬 # ips = self.parseIPList() # proxies = {"http": "{}:8080".format( # ips[random.randint(0, 40)])} # 遍歷,每一頁隨機挑選each_num篇文章 for x in range(each_num): # 隨機抽取每一頁的一篇博文進行訪問,防反爬 current_url = random.choice(each_page_urls) status = True if requests.get( current_url, headers=self.headers).content.decode() else False print("當(dāng)前正在訪問的文章是:{},訪問狀態(tài):{}".format(current_url, status)) time.sleep(1) # 延時1秒,防反爬 time.sleep(1) # 延時1秒,防反爬 i += 1 if __name__ == '__main__': bs = BlogSpider() bs.main(7, 200, 3) # 參數(shù)參照main方法說明,酌情設(shè)置
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決Django Static內(nèi)容不能加載顯示的問題
今天小編就為大家分享一篇解決Django Static內(nèi)容不能加載顯示的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07Python?基于Selenium實現(xiàn)動態(tài)網(wǎng)頁信息的爬取
本文主要介紹了通過Selenium和webdrive等庫,對動態(tài)網(wǎng)頁的信息進行爬取。文中的示例代碼非常詳細(xì),感興趣的同學(xué)快來跟隨小編一起學(xué)習(xí)吧2021-12-12python保留小數(shù)函數(shù)的幾種使用總結(jié)
本文主要介紹了python保留小數(shù)函數(shù)的幾種使用總結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02淺談python 線程池threadpool之實現(xiàn)
這篇文章主要介紹了淺談python 線程池threadpool之實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11使用并行處理提升python?for循環(huán)速度的過程
Python?是一門功能強大的編程語言,但在處理大規(guī)模數(shù)據(jù)或復(fù)雜計算任務(wù)時,性能可能成為一個瓶頸,這篇文章主要介紹了使用并行處理提升python?for循環(huán)速度,需要的朋友可以參考下2023-06-06python3 dict ndarray 存成json,并保留原數(shù)據(jù)精度的實例
今天小編就為大家分享一篇python3 dict ndarray 存成json,并保留原數(shù)據(jù)精度的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12