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.mzph.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…