# -*- coding: utf-8 -*-
import pprint
import requests as req
import re
from bs4 import BeautifulSoup
url = "https://www.taoguba.com.cn/Article/2336826/1"
content = req.get(url).text
s_html = re.findall(r"<!-- 主贴内容开始 -->(.*?)<!-- 主贴内容结束 -->", content, re.S | re.M)[0]
soup = BeautifulSoup(s_html, 'lxml')
imgs = soup.find_all(attrs={'data-type': 'contentImage'})
urls = [img['data-original'] for img in imgs]
s_imgs = re.findall(r"<img.*?/>", s_html) # 非贪婪匹配
match_info = dict(zip(s_imgs, urls))
for s_img in s_imgs:
s_html = s_html.replace(s_img, match_info.get(s_img))
soup = BeautifulSoup(s_html, 'lxml')
text = soup.div.text.strip()
print(pprint.pformat(text))
更新时间: 2019-12-27