遇事不决,可问春风,春风不语,谨遵本心|

布都御魂

园龄:3年9个月粉丝:2关注:1

获取网页的markdown

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 获取网页源码
import re
 
import html2text
import requests
def preprocess_html(html):
    # 删除没有 src 属性的 img 标签
    processed_html = re.sub(r'<img(?![^>]*\ssrc=)[^>]*>', '', html)
    return processed_html
page_url = 'https://www.ysxiao.cn/c/202212/57443.html'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36'
}
def requests_page(url):
    fp = requests.get(url=url, headers=headers, timeout=10)
    fp.encoding = 'utf-8'
    return fp.text
fp = requests_page(page_url)
if isinstance(fp, bytes):
    original_format = fp.decode('utf-8')
else:
    original_format = fp
original_format = preprocess_html(original_format)
markdown = html2text.html2text(original_format)
print(markdown)

  

本文作者:布都御魂

本文链接:https://www.cnblogs.com/wolvies/p/18451333

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   布都御魂  阅读(12)  评论(0编辑  收藏  举报
   
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起