python博客訪問量_史詩級干貨-python爬蟲之增加CSDN訪問量

AI

人工智能

史詩級干貨-python爬蟲之增加CSDN訪問量

史詩級干貨-python爬蟲之增加CSDN訪問量

搜索微信公眾號:‘AI-ming3526’或者’計算機視覺這件小事’ 獲取更多算法、機器學習干貨

csdn:https://blog.csdn.net/baidu_31657889/

github:https://github.com/aimi-cn/AILearners

文章初衷:

最近CSDN官方出了一個流量扶持計劃,針對原創文章進行百度推廣,我嘗試推了幾篇,效果也不是很好,或者是自己文章水平不夠,太水~就想著增加一下自己CSDN的訪問量

想寫出更優質的博客技術文章,不再為了訪問量去寫文章。

本文參照CSDN一個大佬的文章:https://blog.csdn.net/Giser_D/article/details/97472274

加上了使用代理訪問,可以盡量防止被官方封號,更安全一些。

步驟:

在國內髙匿代理IP網站:http://www.xicidaili.com/nn/ 取到ip。

通過解析csdn博客首頁html 獲得相應文章的a標簽鏈接,使用代理對其進行訪問。

Python代碼實現:

#!/usr/bin/env python

# -*- encoding: utf-8 -*-

'''

@File : csdn.py

@Time : 2019/08/26 09:54:47

@Author : xiao ming

@Version : 1.0

@Contact : xiaoming3526@gmail.com

@Desc : None

@github : https://github.com/aimi-cn/AILearners

'''

# 導入相關爬蟲庫和解析xml庫即可

import time

from pyquery import PyQuery as pq

import requests

from bs4 import BeautifulSoup

import random

from fake_useragent import UserAgent

from lxml import etree

import ssl

ssl._create_default_https_context = ssl._create_unverified_context

# 爬取csdn類

class ScrapyMyCSDN:

''' class for csdn'''

def __init__(self,blogname):

'''init 類似于構造函數 param[in]:blogname:博客名'''

csdn_url = 'https://blog.csdn.net/' #常規csdnurl

self.blogurl = csdn_url+blogname #拼接字符串成需要爬取的主頁url

''' Func:獲取寫了多少篇原創文章 '''

''' return:寫了多少篇原創文章'''

def getOriginalArticalNums(self,proxies):

main_response = requests.get(self.blogurl,proxies=proxies)

# 判斷是否成功獲取 (根據狀態碼來判斷)

if main_response.status_code == 200:

print('獲取成功')

self.main_html = main_response.text

main_doc = pq(self.main_html)

mainpage_str = main_doc.text() #頁面信息去除標簽信息

origin_position = mainpage_str.index('原創') #找到原創的位置

end_position = mainpage_str.index('原創',origin_position+1) #最終的位置,即原創底下是數字多少篇博文

self.blog_nums = ''

# 獲取寫的博客數目

for num in range(3,10):

#判斷為空格 則跳出循環

if mainpage_str[end_position + num].isspace() == True:

break

self.blog_nums += mainpage_str[end_position + num]

print(type(str(self.blog_nums)))

cur_blog_nums = (int((self.blog_nums))) #獲得當前博客文章數量

return cur_blog_nums #返回博文數量

else:

print('爬取失敗')

return 0 #返回0 說明博文數為0或者爬取失敗

''' Func:分頁'''

''' param[in]:nums:博文數 '''

''' return: 需要爬取的頁數'''

def getScrapyPageNums(self,nums):

self.blog_original_nums = nums

if nums == 0:

print('它沒寫文章,0頁啊!')

return 0

else:

print('現在開始計算')

cur_blog = nums/20 # 獲得精確的頁碼

cur_read_page = int(nums/20) #保留整數

# 進行比對

if cur_blog > cur_read_page:

self.blog_original_nums = cur_read_page + 1

print('你需要爬取 %d'%self.blog_original_nums + '頁')

return self.blog_original_nums #返回的數字

else:

self.blog_original_nums = cur_read_page

print('你需要爬取 %d'%self.blog_original_nums + '頁')

return self.blog_original_nums

'''Func:開始爬取,實際就是刷瀏覽量hhh'''

'''param[in]:page_num:需要爬取的頁數'''

'''return:0:瀏覽量刷失敗'''

def beginToScrapy(self,page_num,proxies):

if page_num == 0:

print('連原創博客都不寫 爬個鬼!')

return 0

else:

for nums in range(1,page_num+1):

self.cur_article_url = self.blogurl + '/article/list/%d'%nums+'?t=1&' #拼接字符串

article_doc = requests.get(self.cur_article_url,proxies=proxies) #訪問該網站

# 先判斷是否成功訪問

if article_doc.status_code == 200:

print('成功訪問網站%s'%self.cur_article_url)

#進行解析

cur_page_html = article_doc.text

#print(cur_page_html)

soup = BeautifulSoup(cur_page_html,'html.parser')

for link in soup.find_all('p',class_="content"):

#print(link.find('a')['href'])

requests.get(link.find('a')['href'],proxies=proxies) #進行訪問

else:

print('訪問失敗')

print('訪問結束')

# IP地址取自國內髙匿代理IP網站:http://www.xicidaili.com/nn/

#功能:爬取IP存入ip_list列表

def get_ip_list(url, headers):

web_data = requests.get(url, headers=headers)

soup = BeautifulSoup(web_data.text, 'lxml')

ips = soup.find_all('tr')

ip_list = []

for i in range(1, len(ips)):

ip_info = ips[i]

tds = ip_info.find_all('td') #tr標簽中獲取td標簽數據

if not tds[8].text.find('天')==-1:

ip_list.append(tds[1].text + ':' + tds[2].text)

return ip_list

#功能:1,將ip_list中的IP寫入IP.txt文件中

# 2,獲取隨機IP,并將隨機IP返回

def get_random_ip(ip_list):

proxy_list = []

for ip in ip_list:

proxy_list.append(ip)

f=open('IP.txt','a+',encoding='utf-8')

f.write('http://' + ip)

f.write('n')

f.close()

proxy_ip = random.choice(proxy_list)

proxies = {'http':proxy_ip}

return proxies

if __name__ == '__main__':

for i in range(1,3):

url = 'http://www.xicidaili.com/wt/{}'.format(i)

headers = {

'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36'

}

ip_list = get_ip_list(url, headers=headers)

proxies = get_random_ip(ip_list)

print(proxies)

#如何調用該類 參數換成你的csdn名字就行

mycsdn = ScrapyMyCSDN('baidu_31657889') #初始化類 參數為博客名

cur_write_nums = mycsdn.getOriginalArticalNums(proxies) #得到寫了多少篇文章

cur_blog_page = mycsdn.getScrapyPageNums(cur_write_nums) #cur_blog_page:返回需要爬取的頁數

mycsdn.beginToScrapy(cur_blog_page,proxies)

time.sleep(20) # 給它休息時間 還是怕被封號的

需要用到的pip包

我的python環境為3.6.5版本及以上需要安裝相關庫

pip install pyquery

pip install requests

pip install bs4

pip install fake_useragent

pip install lxml

pip install ssl

使用方法

修改主函數第一行中range(1,3),這代表只取兩個隨機代理,然后讓我們的csdn所有原創文章瀏覽量加一遍,循環兩次,修改range后面的值就可以增加循環次數了。

mycsdn = ScrapyMyCSDN('baidu_31657889') #參數為博客名,參數換成你的csdn名字就行

后記

個人感覺提高博客質量才是重點,但是我們可以找到比較好的機會來蹭個熱度,爬一下自己感覺非常不錯的文章。

當然我們要記得適可而止,網上不乏有很多人的號被封的。別忘記我們寫博客的初衷是什么,對訪問量這個東西不用太在意了。

代碼下載地址:https://github.com/aimi-cn/AILearners/tree/master/src/py3.x/others/fm/19.08.26/csdn.py

內容來源于網絡,如有侵權請聯系客服刪除

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/279258.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/279258.shtml
英文地址,請注明出處:http://en.pswp.cn/news/279258.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

弄斷過河電纜_你說的是:剪斷電纜線

弄斷過河電纜Earlier this week we asked you if you’d cut the cable and switched to alternate media sources to get your movie and TV fix. You responded and we’re back with a What You Said roundup. 本周早些時候,我們問您是否要切斷電纜并切換到其他媒…

復制粘貼的句子

Today you do things people will not do,tomorrow you will do things people can not do. 你今天做別人不愿做的事,明天就能做別人做不到的事。轉載于:https://www.cnblogs.com/wensens/p/9723998.html

路由銷毀上一頁_路由器原理(數據通信)

路由:對數據包選擇路徑的過程路由器(也叫網關)智能選擇數據傳輸路由的設備,其端口數量較少!功能:連接網絡1.連接異構網絡以太網、ATM網絡、FDDI網絡2.連接遠程網絡局域網、廣域網隔離廣播將廣播隔離在局域網內路由選擇網絡安全地址…

您可能沒有使用的最佳三星Galaxy功能

Samsung packs its flagship phones with a slew of features—some are even better than stock Android. Either way, there are a lot of things on these phones that you may not be using. Here are some of the best. 包三星旗艦手機用的特性-擺有的甚至比普通的Android…

win7更新錯誤0x800b0109_win7更新漏洞后產生0x0000006B藍屏的解決方法圖解

這幾天不少網友在使用win7更新補丁后就藍屏了,代碼為0x0000006b。發生這一藍屏問題的都是安裝了2016年四月份推出的安全更新補丁,安裝后就出現藍屏,有的網友表示沒問題,有的直接藍了。這個藍屏重啟后依舊,安全模式進不…

獲取構造器的信息

獲取類構造器的用法與上述獲取方法的用法類似,如: import java.lang.reflect.*;public class constructor1 {public constructor1() {}protected constructor1(int i, double d) { } public static void main(String args[]) { try { Class cls Class.f…

如何使用facebook_如果每個人都已經開始使用Facebook,Facebook能否繼續發展?

如何使用facebookThere are only so many people on earth, and so many hours in the day. Is that starting to limit the growth of social media? 地球上只有那么多人,一天中有很多小時。 這是否開始限制社交媒體的增長? Think about how much time…

2018-10-03-Python全棧開發-day60-django序列化-part3

聯合唯一 clean_字段方法只能對某個字段進行檢查,當clean方法執行完之后,最后還會執行clean方法,在clean方法中,可以通過獲取數據字典中的值然后進行驗證 from django.shortcuts import render,HttpResponsefrom django import fo…

mysql時間字段條件查詢_mysql 查詢 時間作為查詢條件

今天select * from 表名 where to_days(時間字段名) to_days(now());昨天SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) - TO_DAYS( 時間字段名) < 1近7天SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) < date(時間字段名)近30天SELECT * FROM 表名 whe…

mac按文件名查找文件_如何在Mac上查找和刪除大文件

mac按文件名查找文件Freeing up disk space on a full hard drive can be difficult, especially when it’s full of small files. However, there are some excellent tools for macOS that let you find the files taking up the most space and delete the ones you don’t…

Swift5.1 語言參考(十) 語法匯總

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★?微信公眾號&#xff1a;山青詠芝&#xff08;shanqingyongzhi&#xff09;?博客園地址&#xff1a;山青詠芝&#xff08;https://www.cnblogs.com/strengthen/&#xff09;?GitHub地址&a…

timestamp mysql php_PHP和Mysql的Timestamp互換

在mysql中有三種時間字段類型&#xff1a;DATETIME&#xff0c;DATE和TIMESTAMP。DATETIME以YYYY-MM-DD HH:MM:SS格式的字符串來保存數據&#xff1b;DATE則是只有年月日以YYYY-MM-DD形式的字串&#xff1b;TIMESTAMP類型和PHP中的TIMESTAMP類型名字一樣&#xff0c;但是兩者基…

dmg是什么文件格式_什么是DMG文件(以及我該如何使用)?

dmg是什么文件格式DMG files are containers for apps in macOS. You open them, drag the app to your Applications folder, and then eject them, saving you the hassle of the dreaded “Install Wizard” of most Windows apps. So if all they are is a folder for an a…

mysql索引三個字段查詢兩個字段_mysql中關于關聯索引的問題——對a,b,c三個字段建立聯合索引,那么查詢時使用其中的2個作為查詢條件,是否還會走索引?...

情況描述&#xff1a;在MySQL的user表中&#xff0c;對a,b,c三個字段建立聯合索引&#xff0c;那么查詢時使用其中的2個作為查詢條件&#xff0c;是否還會走索引&#xff1f;根據查詢字段的位置不同來決定&#xff0c;如查詢a, a,b a,b,c a,c 都可以走索引的&#…

HDU 3966 Aragorn's Story (樹鏈剖分+線段樹)

題意&#xff1a;給你一棵樹&#xff0c;然后有三種操作 I L R K: 把L與R的路徑上的所有點權值加上K D L R K&#xff1a;把L與R的路徑上的所有點權值減去K Q X&#xff1a;查詢節點編號為X的權值 思路&#xff1a;樹鏈剖分裸題&#xff08;我還沒有怎么學懂&#xff0c;但基本…

canon相機api中文_您應該在佳能相機上掌握的10種相機設置

canon相機api中文Your camera is a tool, and you should be able to use it with total confidence. You should never have to dig through the manual or play around with random buttons trying to work out how to do something on a shoot. Here are the most important…

mysql普通索引自增_mysql中聯合索引中的自增列的增長策略

《深入理解MySQL》中一段介紹MyISAM存儲引擎中自動增長列的示例,如下1 mysql>create table autoincre_demo2 -> (d1 smallint not nullauto_increment,3 -> d2 smallint not null,4 -> name varchar(10),5 ->index(d2,d1)6 -> )enginemyisam;7 Query OK, 0 r…

spring-boot基礎概念與簡單應用

1.spring家族 2.應用開發模式 2.1單體式應用 2.2微服務架構 微服務架構中每個服務都可以有自己的數據庫 3.微服務架構應當注意的細節 3.1關于"持續集成,持續交付,持續部署" 頻繁部署、快速交付以及開發測試流程自動化都將成為未來軟件工程的重要組成部分 可行方案(如…

郵箱客戶端 gmail支持_如何聯系Gmail支持

郵箱客戶端 gmail支持Although you may not be able to directly contact Gmail support without subscribing to G Suite for businesses, there are a couple of ways to get the answers you’re looking for online. Let’s look at how you can get help with your Gmail …

jstorm mysql_zookeeper,kafka,jstorm,memcached,mysql流式數據處理平臺部署

一&#xff0e;平臺環境介紹:1&#xff0e;系統信息&#xff1a;項目信息系統版本:Ubuntu14.04.2 LTS \n \l用戶&#xff1a;*****密碼&#xff1a;******Java環境&#xff1a;openjdk-7-jre語言&#xff1a;en_US.UTF-8&#xff0c;en_US:en磁盤&#xff1a;每臺vda為系統盤(5…