Python入门学习之:10分钟1500访问量

看效果:

在这里插入图片描述

不扯没用的,直接上代码:

# author   : sunzd
# date     : 2019/9/01
# position : beijing

from fake_useragent import UserAgent
from bs4 import BeautifulSoup
from urllib import request
from urllib import error
import re
import time


def html_request(url):
    if url is None:
        return
    print("download html is :{0}".format(url))
    # 如果url包含中文,则需要进行编码

    # 模拟浏览器行为
    headers = {'UserAgent': str(UserAgent().random)}
    req = request.Request(url, headers=headers)

    try:
        html = request.urlopen(req).read().decode('utf-8')
    except error.URLError as e:
        if hasattr(e, "code"):
            print(e.code)
        if hasattr(e, "reason"):
            print(e.reason)
        return None
    # print(html)
    return html


def html_parser(url, html):
    if url is None or html is None:
        return
    # pattern = '<main>(.+?)</main>'   #因为<main>后紧跟的时‘\n’因此需要忽略掉使用模式修正符re.S使'.'可以匹配任意字符
    # articles = re.compile(pattern, re.S).findall(html)
    # articles = articles[0]

    pattern_art = '<div class="article-item-box csdn-tracking-statistics" data(.+?)</div>'
    # print(articles)
    articles = re.compile(pattern_art, re.S).findall(html.replace('\n', ''))
    print(articles.__len__())
    for article in articles:
        soup = BeautifulSoup(article, 'html.parser')
        title = soup.find('a', attrs={'target': '_blank'})
        # print(title)
        print(
            "文章题目:{0}\n文章类型:{1}".format(title.text.replace(' ', '').replace("原", "").replace("转", ""), title.span.text))
        print("文章链接:{0}".format(title.attrs['href']))
        html_request(title.attrs['href'])
        infors = soup.find('div', attrs={'class': 'info-box d-flex align-content-center'})
        # for infor in infors.p.next_siblings:   next_siblings : 因为不包括自己,因此会把第一个p节点信息去掉。
        # for infor in infors.children:
        #     if infor == ' ':  # ‘ ’空格也会识别为他的孩子,因此需要过滤掉
        #         continue
        #     # print("======{0}".format(infor))
        #     if infor.span:  # 只需要<span >节点的信息
        #         print("{0}".format(infor.span.text))

    pattern_next = '<li class="js-page-next js-page-action ui-pager ui-pager-disabled">'
    next = re.compile(pattern_next).findall(html)
    # print(html)
    print("是否为最后一页:{0}----{1}".format(len(next), next))
    if len(next) == 0:
        return 0
    else:
        return 0


if __name__ == '__main__':
    name = '你自己的名称'
    page = 1
    url = "https://blog.csdn.net/" + name + "/article/list/" + str(page) + '?'
    while page < 7:
        html = html_request(url)
        # print(html)
        next = html_parser(url, html)
        page += 1
        if page > 6:
            page = 1
        url = "https://blog.csdn.net/" + name + "/article/list/" + str(page) + '?'

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叨陪鲤

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值