如何用正则表达式匹配汉字?
一般情况下可以这样匹配中文,如图:<img src="https://pic4.zhimg.com/50/edcbd2faf1a916675cec852bd886e599_hd.jpg" data-rawwidth="827" data-rawheight="600" class="origin_image zh-lightbox-thumb" width="827" data-original="https://pic4.zhimg.com/edcbd2faf1a916675cec852bd886e599_r.jpg">
先用靓汤或正则找到这个节点,再用上面的字符组匹配。
假设这个节点只有一个,用法如下:
import reimport requests as reqfrom bs4 import BeautifulSoupurl = 'xxx'html = req.get(url).textbs = BeautifulSoup(html)span = bs.find_all('span', 'pro-title')'''span = re.findall('<span\sclass="pro-title">[^<]+</span>', html)s = span[0]m = re.findall('[\u4e00-\u9fa5]+', s)'''s = str(span)m = re.findall('[\u4e00-\u9fa5]+', s)print(m)