您的位置:首页 > 科技 > IT业 > 网店网络营销策划方案_网上推广服务_培训方案_重庆森林台词

网店网络营销策划方案_网上推广服务_培训方案_重庆森林台词

2025/4/30 3:39:31 来源:https://blog.csdn.net/cf8833/article/details/147496343  浏览:    关键词:网店网络营销策划方案_网上推广服务_培训方案_重庆森林台词
网店网络营销策划方案_网上推广服务_培训方案_重庆森林台词

博客爬虫算法

我希望从某些网站,把博客文章保存成本地的md文件,用python实现
不管你怎么想,反正我是成功了

step1:C:\Users\wangrusheng\PycharmProjects\FastAPIProject1\hello.py

import requests
from bs4 import BeautifulSoup
import html2text  # 新增HTML转Markdown库# 目标文章URL
url = 'https://blog.csdn.net/cf8833/article/details/147413479'# 设置请求头模拟浏览器
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}try:# 发送HTTP请求response = requests.get(url, headers=headers)response.raise_for_status()# 解析HTML内容soup = BeautifulSoup(response.text, 'html.parser')# 查找文章主体article_content = soup.find('div', {'id': 'content_views'})if article_content:# 创建HTML转Markdown转换器h = html2text.HTML2Text()h.body_width = 0  # 禁用自动换行h.emphasis_mark = '*'  # 设置斜体符号h.strong_mark = '**'  # 设置加粗符号h.ul_item_mark = '-'  # 设置无序列表符号# 转换HTML内容为Markdownmd_content = h.handle(str(article_content))# 保存到文件with open('csdn_article2.md', 'w', encoding='utf-8') as f:f.write(md_content)print('文章已成功保存为Markdown格式!')else:print('未找到文章内容,请检查页面结构。')except requests.exceptions.RequestException as e:print(f'请求出错:{e}')
except Exception as e:print(f'发生错误:{e}')

end

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com