爬虫技巧 --图片文字混采,图片位置替换为 url 字符串

# -*- 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

发布了291 篇原创文章 · 获赞 104 · 访问量 41万+

猜你喜欢

转载自blog.csdn.net/Enjolras_fuu/article/details/103732643