python爬蟲增加訪問量的方法
看著自己少得可憐的訪問量,突然有一個(gè)想用爬蟲刷訪問量的想法,主要也是抱著嘗試的心態(tài),學(xué)習(xí)學(xué)習(xí)。
其實(shí)市面上有一些軟件可以代刷流量 比如 流量精靈,使用感確實(shí)比我們自己寫的代碼要好一些
第一版:網(wǎng)上借鑒了一下 以下代碼運(yùn)行在 python3
import urllib.request
import time
# 使用build_opener()是為了讓python程序模仿瀏覽器進(jìn)行訪問
opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
# 專刷某個(gè)頁(yè)面
print('開始刷了哦:')
tempUrl = 'https://blog.csdn.net/Lin_QC/article/details/88966839'
for j in range(2000):
try:
opener.open(tempUrl)
time.sleep(7)
print('%d %s' % (j, tempUrl))
except urllib.error.HTTPError:
print('urllib.error.HTTPError')
time.sleep(1)
except urllib.error.URLError:
print('urllib.error.URLError')
time.sleep(1)
該代碼主要就是利用爬蟲打開網(wǎng)頁(yè)來進(jìn)行訪問量的刷新,但是,該方法遇到了瓶頸,當(dāng)刷新到一定訪問量時(shí),csdn的服務(wù)器會(huì)阻止該ip的訪問,也就刷新不了訪問量了。
所以,也就衍生了第二版。
我們可以在 https://www.xicidaili.com 網(wǎng)站上看到很多代理ip,使用這些代理ip,可以防止csdn服務(wù)器阻止訪問。
首先,編寫了一個(gè)獲取代理ip的文件,經(jīng)我本人實(shí)驗(yàn),國(guó)內(nèi)http代理ip較為穩(wěn)定,所以我們爬取
'https://www.xicidaili.com/wt/1
頁(yè)面的代理ip信息,并將它們存儲(chǔ)在proxy文件里,以下代碼是基于 python2的,注意不要弄錯(cuò)版本
proxy_IP.py文件
import urllib2
import BeautifulSoup
User_Agent = 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0'
header = {}
header['User-Agent'] = User_Agent
url = 'https://www.xicidaili.com/wt/1'
req = urllib2.Request(url, headers=header)
res = urllib2.urlopen(req).read()
soup = BeautifulSoup.BeautifulSoup(res)
ips = soup.findAll('tr')
f = open("proxy", "w")
for x in range(1,len(ips)):
ip = ips[x]
tds = ip.findAll("td")
ip_temp = tds[1].contents[0]+","+tds[2].contents[0]+"\n"
print tds[1].contents[0]+"\t"+tds[2].contents[0]
f.write(ip_temp)
通過執(zhí)行以上代碼,我們就可以獲得大量代理ip,接下來就是使用這些ip進(jìn)行對(duì)博客的訪問。
csdnfake.py
import urllib2
import socket
import time
import random
socket.setdefaulttimeout(3)
user_agent_list = [
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/45.0.2454.85 Safari/537.36 115Browser/6.0.3',
'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50',
'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50',
'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)',
'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)',
'Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1',
'Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11',
'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SE 2.X MetaSr 1.0; SE 2.X MetaSr 1.0; .NET CLR 2.0.50727; SE 2.X MetaSr 1.0)',
'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0',
'Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1',
]
f = open("proxy")
lines = f.readlines()
proxys = []
for i in range(0,len(lines)):
ip = lines[i].strip().split(",")
proxy_host = "http://"+ip[0]+":"+ip[1]
print "http://"+ip[0]+":"+ip[1]
proxy_temp = {"http": proxy_host}
proxys.append(proxy_temp)
urls = {"https://blog.csdn.net/Lin_QC/article/details/88966839",
"https://blog.csdn.net/Lin_QC/article/details/88930018",
"https://blog.csdn.net/Lin_QC/article/details/88642949",
"https://blog.csdn.net/Lin_QC/article/details/84568170",
"https://blog.csdn.net/Lin_QC/article/details/84451279",
"https://blog.csdn.net/Lin_QC/article/details/84927503",
}
j=1
for i in range(100):
for proxy in proxys:
for url in urls:
try:
user_agent = random.choice(user_agent_list)
proxy_support = urllib2.ProxyHandler(proxy)
opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler)
urllib2.install_opener(opener)
req = urllib2.Request(url)
c = urllib2.urlopen(req)
print ("sucessful",j)
j+=1
time.sleep(5)
except Exception, e:
print proxy
print e
continue
user_agent_list是一堆瀏覽器的代理頭,可以模仿瀏覽器訪問博客。
每次訪問休息五秒,主要是因?yàn)檫^快的訪問對(duì)csdn無效。
總結(jié)
以上所述是小編給大家介紹的python爬蟲增加訪問量的方法,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
相關(guān)文章
Python基于socket實(shí)現(xiàn)簡(jiǎn)單的即時(shí)通訊功能示例
這篇文章主要介紹了Python基于socket實(shí)現(xiàn)簡(jiǎn)單的即時(shí)通訊功能,涉及Python基于socket模塊實(shí)現(xiàn)tcp通信客戶端與服務(wù)器端相關(guān)操作技巧,需要的朋友可以參考下2018-01-01
python使用fileinput模塊實(shí)現(xiàn)逐行讀取文件的方法
這篇文章主要介紹了python使用fileinput模塊實(shí)現(xiàn)逐行讀取文件的方法,涉及Python中fileinput模塊操作文件的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
python3.6連接mysql數(shù)據(jù)庫(kù)及增刪改查操作詳解
這篇文章主要介紹了python3.6 連接mysql數(shù)據(jù)庫(kù)及增刪改查操作詳解,需要的朋友可以參考下2020-02-02
Python生成器實(shí)現(xiàn)簡(jiǎn)單"生產(chǎn)者消費(fèi)者"模型代碼實(shí)例
這篇文章主要介紹了Python生成器實(shí)現(xiàn)簡(jiǎn)單"生產(chǎn)者消費(fèi)者"模型代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
PyCharm刷新項(xiàng)目(文件)目錄的實(shí)現(xiàn)
今天小編就為大家分享一篇PyCharm刷新項(xiàng)目(文件)目錄的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02
Python 根據(jù)相鄰關(guān)系還原數(shù)組的兩種方式(單向構(gòu)造和雙向構(gòu)造)
本文主要介紹了Python 根據(jù)相鄰關(guān)系還原數(shù)組的兩種方式,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07

