爬虫技巧 --图片文字混采,图片位置替换为 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值