Home  >  Article  >  Backend Development  >  How to use python to increase traffic

How to use python to increase traffic

little bottle
little bottleforward
2019-04-09 14:53:173449browse

Looking at the pitifully low number of visits to the blog I wrote, did I suddenly have the idea of ​​ increasing the number of visits? Then the editor found a code from the Internet and tried to increase the number of visits. But I felt deeply guilty after finishing the game. We should not achieve success through sideways. Just give this code a try, and don’t use it to do bad things!

First of all, import urllib2 is required. For codecs, I originally thought that I needed to convert the output format, but later I found out that it was not necessary. re is used for regular expression matching. In order to look more comfortable, I defined a CSDN class. __init__(self) is used to assign the initial value. Since I am lazy, I directly copy and paste the address of each blog post (in this way, the number of visits to each blog post will increase at the same time. One blog post will not be 10,000, and the others will be 10 A tragic situation, but I declare here that my blog had 6,000 visits before I posted it, but it has just doubled... It inspires me to learn better!), but you can also enter csdn_url and use regular expressions The expression automatically obtains the address of each blog post. In addition, we must disguise a header, otherwise the website will not let you enter. So at first I wanted to use urllib.urlopen(csdn_url).read(), but I found that the text I got was forbidden to access! And in order to directly observe the drastic changes in the number of visits to our blog, I set up an openCsdn function and used regular expressions to find the number of visits. Not much to say, the code is proof!

#-*- coding=utf-8 -*-

import urllib2
import codecs
import re

csdn_url = "http://blog.csdn.net/walegahaha"
blog_url = ["http://blog.csdn.net/walegahaha/article/details/51945421",
	   "http://blog.csdn.net/walegahaha/article/details/51867904",
	   "http://blog.csdn.net/walegahaha/article/details/51603040",
	   "http://blog.csdn.net/walegahaha/article/details/50938260",
	   "http://blog.csdn.net/walegahaha/article/details/50884627",
	   "http://blog.csdn.net/walegahaha/article/details/50877906",
	   "http://blog.csdn.net/walegahaha/article/details/50868049",
	   "http://blog.csdn.net/walegahaha/article/details/50533424",
	   "http://blog.csdn.net/walegahaha/article/details/50504522",
	   "http://blog.csdn.net/walegahaha/article/details/50489053",
	   "http://blog.csdn.net/walegahaha/article/details/50471417",
	   "http://blog.csdn.net/walegahaha/article/details/50464531",
	   "http://blog.csdn.net/walegahaha/article/details/50452959",
	   "http://blog.csdn.net/walegahaha/article/details/50435986",
	   ]

class CSDN(object):
	def __init__(self):
		self.csdn_url = csdn_url
		self.blog_url = blog_url
		self.headers =  {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6',}  

	def openCsdn(self):
		req = urllib2.Request(self.csdn_url, headers = self.headers)
		response = urllib2.urlopen(req)
		thePage = response.read()
		response.close()
		pattern = "访问:(\d+)次"
		number = ''.join(re.findall(pattern, thePage))
		print number

	def openBlog(self):
		for i in range(len(self.blog_url)):
			req = urllib2.Request(self.blog_url[i], headers = self.headers)
			response = urllib2.urlopen(req)
			response.close()

	
for i in range(500):
	print i
	csdn = CSDN()
	csdn.openCsdn()
	csdn.openBlog()
	csdn.openCsdn()

【Recommended course: Python video tutorial

The above is the detailed content of How to use python to increase traffic. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete