Rumah > pembangunan bahagian belakang > Tutorial Python > 正则表达式如何匹配网页里面的汉字?

正则表达式如何匹配网页里面的汉字?

WBOY
Lepaskan: 2016-06-06 16:23:34
asal
2359 orang telah melayarinya

python做的爬虫,需要提取html中Apple iPhone 5s (A1530) 16GB 金色 移动联通4G手机 这里面的手机名,因为初学怎么都不能匹配完全, 其中用.*只能匹配到Apple iPhone 5s (A1 请各位指点一下!已经困住两天了!

回复内容:

谢邀。
我又来安利xpath了,放弃正则表达式吧少年。
<code class="language-text">//span[@class="pro-title"]/text()
</code>
Salin selepas log masuk
没人用BeautifulSoup?
(?).*?(?=
如果实际情况中有许多不同的“”对,就请自行填充前后向断言的内容 你应该看看汉字编码,网页的编码很可能是GBK,然而python是用的utf8,所以绝对匹配不了 正则表达式如何匹配网页里面的汉字?
怒答,看到不懂正则的还瞎BB,为你感到悲哀!上图!
<code class="language-text">---------------代码区----------------
# coding:utf-8
import re
x='<span class="pro-title">Apple iPhone 5s (A1530) 16GB 金色 移动联通4G手机</span> <span class="pro-price">'
xre=r'(<span class="pro-title">)(.+)(</span> <span class="pro-price">)'
z=re.search(xre,x).group(2)
print z
----------输出区-----------------------
C:\Python27\python.exe D:/PycharmProjects/爬虫/test.py
Apple iPhone 5s (A1530) 16GB 金色 移动联通4G手机

进程已结束,退出代码0
</span></span></code>
Salin selepas log masuk
.+ /sSU 一般情况下可以这样匹配中文,如图: 正则表达式如何匹配网页里面的汉字?
先用靓汤或正则找到这个节点,再用上面的字符组匹配。
假设这个节点只有一个,用法如下:
<code class="language-python3"><span class="kn">import</span> <span class="nn">re</span>
<span class="kn">import</span> <span class="nn">requests</span> <span class="k">as</span> <span class="nn">req</span>
<span class="kn">from</span> <span class="nn">bs4</span> <span class="k">import</span> <span class="n">BeautifulSoup</span>

<span class="n">url</span> <span class="o">=</span> <span class="s">'xxx'</span>
<span class="n">html</span> <span class="o">=</span> <span class="n">req</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">url</span><span class="p">)</span><span class="o">.</span><span class="n">text</span>
<span class="n">bs</span> <span class="o">=</span> <span class="n">BeautifulSoup</span><span class="p">(</span><span class="n">html</span><span class="p">)</span>
<span class="n">span</span> <span class="o">=</span> <span class="n">bs</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="s">'span'</span><span class="p">,</span> <span class="s">'pro-title'</span><span class="p">)</span>

<span class="sd">'''</span>
<span class="sd">span = re.findall('<span>[^', html)</span>
<span class="sd">s = span[0]</span>
<span class="sd">m = re.findall('[\u4e00-\u9fa5]+', s)</span>
<span class="sd">'''</span>

<span class="n">s</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">span</span><span class="p">)</span>
<span class="n">m</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s">'[</span><span class="se">\u4e00</span><span class="s">-</span><span class="se">\u9fa5</span><span class="s">]+'</span><span class="p">,</span> <span class="n">s</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
</span></code>
Salin selepas log masuk
pyquery 会用jquery的话你会爱上它的 换php用 phpQuery CSS Selector

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan