首页 > 编程语言 >Python爬取用户所有博客

Python爬取用户所有博客

时间:2023-06-27 14:33:33浏览次数:36  
标签:title Python text 博客 爬取 url article sel page

CSDN 的爬取比较简单,没有知乎那种反爬虫需要 ip 代理模拟登录那么麻烦。在确认一个用户之后,找到目录的 url,再通过 css 选择器找到每一篇博客的 url 再分别保存为 markdown 格式。

import requests
import parsel
import tomd
import re

先导入需要用到的包。第一次用到 tomd,用于把 html 格式文件转化成为 markdown 格式,但仍有部分标签保留,如<a><br>,re 库挑选出这两种保留标签并分别替换为空字符串和换行符。

def download_url(article_url):
    response = requests.get(article_url)
    html = response.text
    sel = parsel.Selector(html)
    title = sel.css('.title-article::text').get()
    content = sel.css('article').get()
    text = tomd.Tomd(content).markdown
    text = re.sub('<a.*?a>', "", text)
    text = re.sub('<br>', "\n", text)
    # print(title)
    # print(text)
    with open(title + ".md", mode='w', encoding='utf-8') as f:
        f.write("#" + title)
        f.write(text)

def download_user():
    page = 1
    while True:
        user_name = 'fei347795790'
        index_url = 'https://blog.csdn.net/{}/article/list/{}?'.format(user_name, page)
        response = requests.get(index_url)
        html = response.text
        sel = parsel.Selector(html)
        urls = sel.css('.article-list a::attr(href)').getall()
        if not urls:
            break;
        print("第", page, "页")
        for url in urls[2:]:
            download_url(url)
            print(url)
        page = page + 1

download_user()

标签:title,Python,text,博客,爬取,url,article,sel,page
From: https://www.cnblogs.com/dirtycat/p/17508776.html

相关文章

  • (Python编程)"添加Python,充分混和。"
    ProgrammingPython,3rdEdition翻译最新版本见:http://wiki.woodpecker.org.cn/moin/PP3eD23.1."AddPython.MixWell.Repeat."23.1."添加Python,充分混和。"Inthepriorchapter,weexploredhalfofthePython/Cintegration......
  • (Python编程)集成的方式
    ProgrammingPython,3rdEdition翻译最新版本见:http://wiki.woodpecker.org.cn/moin/PP3eD22.2.IntegrationModes22.2.集成的方式ThelasttwotechnicalchaptersofthisbookintroducePython'stoolsforinterfacingtotheoutsideworldanddiscussbothit......
  • Python:中文域名的编码处理
    中文域名通过https://whois.aliyun.com/domain/百度.中国域名信息查询(WHOIS)结果如下DomainName:百度.中国PunyName:xn--wxtr44c.xn--fiqs8s中文域名处理print('中国'.encode('punycode'))#b'fiqs8s'print('百度.中国'.encode('punycode'))......
  • 【AGC】云数据库云侧Python SDK集成使用方法
    使用场景云数据库服务端以前支持Node.JS和Java平台的ServerSDK。现在介绍一下服务端为Python平台时的使用方法。集成准备Python环境配置1.下载Python和PyCharm并安装。2.使用安装的python本身作为解释器。3.安装AGCPythonSDK。AGC环境配置1.在AGC创建项目和应用2.开通云数据库服......
  • python-docx - 3
    1.添加图片函数:add_picture(图片文件,width=宽,height=高)如果只指定一个宽或高,则按比例缩放。1.1文档直接添加图片fromdocximportDocumentfromdocx.sharedimportCmdoc=Document()doc.add_picture("images/2.jpg",width=Cm(8))doc.save("1.docx")1.2段落......
  • python批量安装第三方库
    把需要安装的库名和版本号(版本号可写)写入txt文件: 终端输入pip安装命令,等待安装完成即可:pipinstall-rrequirements.txt-i......
  • ubuntu搭建python3.10.7(服务器第一步)
    一安装python更新Ubuntu软件源和依赖(建议操作)1-sudoaptupdate2-sudoaptinstallbuild-essentialzlib1g-devlibncurses5-devlibgdbm-devlibnss3-devlibssl-devlibreadline-devlibffi-dev3-下载安装包:wgethttps://www.python.org/ftp/python/3.10.7/Python-3.10.7......
  • python 批量删除 redis 大量数据
    #!/usr/bin/envpython#ScananddeletekeysinRedis.#Author:cdfivefromredisimportRedisimporttimedefRedisScanAndDelete(host,port,password,db,cursor,pattern,count,batch_delete_size):start_time=time.time()client=Redis(host......
  • python:一行代码读写文件
    1、读取文件lst=[line.strip()forlineinopen('data.txt')]print(lst)这里我们使用列表来处理。首先,我们打开一个文本文件,并使用for循环,逐行读取。最后,使用strip删除所有不必要的空间。通过使用列表功能,使得代码更简单,更短。list(open('data.txt'))##Usingwithwi......
  • Python -Flask HTML <img 显示本地图片失败,怎么破?
    大家好,我是皮皮。一、前言前几天在Python白银群【膨】问了一个Flask图片显示的问题,这里拿出来给大家分享下。运行之后图片加载不出来。二、实现过程这里【此类生物】给了一个思路,flask运行当前路径是主程序路径,把图片路径改成绝对路径。结果页面还是加载不出来。后来以......