Python一日一练05----怒刷点击量

功能

自己主动获取CSDN文章列表,并对每篇文章添加点击量.

源代码

import urllib.request
import re
import time
import random
from bs4 import BeautifulSoup

p = re.compile('/a359680405/article/details/........')

#自己的博客主页
url = "http://blog.csdn.net/a359680405"

#使用build_opener()是为了让python程序模仿浏览器进行訪问
opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]

html = opener.open(url).read().decode('utf-8')

allfinds = p.findall(html)
print(allfinds)

urlBase = "http://blog.csdn.net"#须要将网址合并的部分
#页面中的网址有反复的,须要使用set进行去反复
mypages = list(set(allfinds))
for i in range(len(mypages)):
    mypages[i] = urlBase+mypages[i]

print('要刷的网页有:')
for index , page in enumerate(mypages) :
    print(str(index), page)

#设置每一个网页要刷的次数
brushMax = 200

#全部的页面都刷
print('以下開始刷了哦:')
for index , page in enumerate(mypages) :
    brushNum=random.randint(0,brushMax)
    for j in range(brushNum):
        try :
            pageContent = opener.open(page).read().decode('utf-8')
            #使用BeautifulSoup解析每篇博客的标题
            soup = BeautifulSoup(pageContent)
            blogTitle = str(soup.title.string)
            blogTitle = blogTitle[0:blogTitle.find('-')]
            print(str(j) , blogTitle) 

        except urllib.error.HTTPError:
            print('urllib.error.HTTPError')
            time.sleep(1)#出现错误。停几秒先

        except urllib.error.URLError:
            print('urllib.error.URLError')
            time.sleep(1)#出现错误,停几秒先
        time.sleep(0.1)#正常停顿,以免server拒绝訪问



时间: 2024-08-24 07:54:16

Python一日一练05----怒刷点击量的相关文章

Python一日一练102----创建简易博客(下)

继续改博客 接着上一篇继续改咱的博客. 母语不是英语肿么办,博客上面英语好多啊! 更改很简单. 你可以做一个快速更改来观察Django管理工具是否被翻译成你想要的语言. 仅需在settings.py添加'django.middleware.locale.LocaleMiddleware' 到MIDDLEWARE_CLASSES设置中,并确保它在'django.contrib.sessions.middleware.SessionMiddleware'之后就可以了. 建立博客的前台部分 创建模版

Python一日一练06----怒刷点击量pyqt5版

功能 输入博主用户名,增加博主点击量 软件截图 部分源码 # coding=utf-8 __author__ = 'a359680405' from PyQt5 import QtWidgets from PyQt5.QtCore import QThread from PyQt5.QtCore import pyqtSignal from visitspage.UI.MainForm import Ui_MainForm from visitspage.UI.newForm import Ui

Python 一日一练: 01

将数字转换为LCD模式 代码如下: 1 # -*- coding:utf-8 -*- 2 ''' 3 Python 小练习-1 : 将数字转换为LCD显示 4 ''' 5 lt = [' __ ', '|__ ', '|__|', ' |', ' __|', '| |', ' '] 6 dt = {0:[0, 5, 2],1:[6, 3, 3], 2:[0, 4, 1], 3:[0, 4, 4], 4:[6, 2, 3], 5:[0, 1, 4], 6:[0, 1, 2], 7:[0, 3, 3

Python一日一练103----Django模版练习

要求 request.META 是一个Python字典,包含了所有本次HTTP请求的Header信息,比如用户IP地址和用户Agent.通过request.META.items()可得到这个字典.要求将META信息输出到网页. 输出结果 源码 index.html <html> <head><title>{{index_title}}</title></head> <body> {% block mainbody %} {%for k

Python一日一练01----字符输出

要求 编写一个程序,当输入python bigdigists.py 234234,会输出下列结果. 源码 import sys Zero = ["  ***  ",         " *   * ",         "*     *",         "*     *",         "*     *",         " *   * ",         "  *

Python一日一练104----Django URLconf练习

要求 请实现当url是http://127.0.0.1:8000/sum/n/(n是数字)时,返回的页面显示1-n的累加和的结果. 源码 views.py文件 #coding:utf-8 from django.shortcuts import render from django.http import HttpResponse,Http404 # Create your views here. def num_plus(request,plus): try: plus=int(plus) e

Python一日一练03----输出网页

要求 将以下文档作为网页模版,编程实现由用户定义网页信息并将网页输出保存 <?xml version="1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" \ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/199

Python一日一练03----一元二次方程的计算

要求 由用户随意给定一元二次方程系数a.b.c,计算一元二次方程的解 源码 import cmath #包含复数运算 import math import sys def get_float(msg, allow_zero): x = None while x is None: try: x = float(input(msg)) if not allow_zero and abs(x) < sys.float_info.epsilon: #sys.float_info.epsilon是浮点数0

Python一日一练02----诗词生成器

要求 编写一段程序,可以自动生成小诗. 格式如下 源码 import random import sys articles = ["the", "a", "another", "her", "his"] subjects = ["cat", "dog", "horse", "man", "woman", &q