python 利用urllib抓html源码后,字符串匹配问题

Engandget
  • 23

我用python请求一个网页,然后判断一段字符串是否在返回的html源码当中,代码如下:

import urllib.request
response = urllib.request.urlopen('http://product.suning.com/127544661.html?srcPoint=ssdln_20334_pro_name30-2_0_0_127544661_0')
html = response.read()
con = str(html)

a=r"<span>型号</span>"


if str(a.encode('utf-8')) in  con:
    print('存在')

按道理,应该输出'存在',但是没有哈,请大神指教,不知道是否是编码的问题,谢谢!

回复
阅读 3.4k
1 个回答
# -*- coding:utf-8 -*-
import urllib.request
response = urllib.request.urlopen('http://product.suning.com/127544661.html?srcPoint=ssdln_20334_pro_name30-2_0_0_127544661_0')
html = response.read()
a="<span>型号</span>"
if a in html:
    print('存在')

宣传栏