主程式
# tools是我的自定義工具類
import tools
import socket
'''模擬訪問部落格增加訪問量
'''socket.setdefaulttimeout(1.0)
visitnum = 2
if len(sys.argv) > 1:
visitnum = sys.argv[1]
visitnum = int(visitnum)
class csdn(threading.thread):
'csdn增加訪問量'
contents = {}
headers = [('host', 'blog.csdn.net'),
('user-agent',
]domain = ''
url = '/%s/article/list/%s'
def __init__(self, username, page, times):
threading.thread.__init__(self)
self.username = username
self.createopener()
self.page = page
self.times = times
@staticmethod
def getlistpages(username,page=1,count=1):
'獲取總頁數'
raise exception('出錯了')
csdn.contents[key] = contents = response.read().decode('utf-8', 'ignore')
pattern = r'([\s\s]*?)共(\d+)頁'
'訪問列表頁獲取內容'
key = 'page_' + str(self.page)
if key in csdn.contents:
print('--------%s exists' % key)
self.contents = csdn.contents[key]
else:
opener = self.opener
url = csdn.url % (self.username, self.page)
try:
response = opener.open(url)
self.contents = contents = response.read().decode('utf-8', 'ignore')
except exception as e:
print("訪問 %s 出錯 " % url)
return
csdn.contents[key] = contents
self.addvisitnum()
def addvisitnum(self):
opener = self.opener
contents = self.contents
divpattern = r''
ulmatch = re.search(divpattern, contents)
divtext = ulmatch.group(1)
smallpattern = r'([\s\s]*?)([\s\s]*?)
([\s\s]*?)閱讀\((\d+)\)'
match = re.findall(smallpattern, divtext)
for i in match:
list =
try:
opener.open(list['url'])
except exception as e:
print("當前第%s次訪問,訪問出錯,url:%s" % (self.times, list['url']))
continue
print("當前第%s次訪問,第%s頁,url:%s" % (self.times, self.page, list['url']))
def run(self):
'執行緒主方法'
self.visiturl()
class maincsdn(threading.thread):
def __init__(self, username, times, pages):
threading.thread.__init__(self)
self.username = username
self.times = times
self.pages = pages
def run(self):
pages = self.pages
username = self.username
threads =
for page in range(1, pages + 1):
thread = csdn(username, page, times=self.times)
thread.start()
# 等待所有執行緒完成
for t in threads:
t.join()
print(self.times, " 退出第%s次執行任務" % self.times)
@tools.runtime
def main():
'主方法'
# csdn暱稱
username = 'csdn暱稱'
pages = csdn.getlistpages(username,1,1)
threads =
# startthread = maincsdn(username=username, times=0, pages=pages)
# startthread.start()
# startthread.join()
for i in range(1, visitnum + 1):
thread = maincsdn(username=username, times=i, pages=pages)
thread.start()
# 等待所有執行緒完成
for t in threads:
t.join()
print("退出主線程")
if __name__ == '__main__':
main()
tools.py
#!/usr/bin/python
# -*- coding:utf-8 -*-
import time
'''自定義工具方法,tools.py
'''def runtime(func):
'記錄程式執行時間'
def newfunc(*args, **kwargs):
start = time.clock()
res = func(*args, **kwargs)
end = time.clock()
print("read: %f s" % (end - start))
return res
return newfunc
def log(content, file='test.log', type=1):
if type == 1:
f = open(file, 'a+', encoding='utf-8')
else:
f = open(file, 'w+', encoding='utf-8')
content=str(content)
f.write(content)
Python刷csdn訪問量
正所周知,python是一門很強大的語言,尤其在抓取資料方面。既然能抓取資料,這就說明可以向 傳送請求。所以基於python語言,借鑑網上的一些資料,大體實現了小規模的訪問量的提公升。廢話少說,我直接上 import urllib.request,re from bs4 import beautif...
python 刷csdn部落格訪問量
由於同個ip訪問短時間不能重複訪問量不會增加,於是選取多篇文章訪問,一邊下來,差不多第一篇文章便可以訪問了。以下 只設定將文章列表訪問以次。防止封號,僅供學習。coding utf 8 import webbrowser as web import time import os urllist fo...
Python爬蟲 爬取CSDN訪問量
import requests from bs4 import beautifulsoup 構造請求頭 headers 傳送get請求 r requests.get headers headers 處理 soup beautifulsoup r.text,html.parser 查詢class t ...