我正在尝试使用regex python从web页面获取代理

2024-09-27 07:33:26 发布

您现在位置:Python中文网/ 问答频道 /正文

import urllib.request
import re
page = urllib.request.urlopen("http://www.samair.ru/proxy/ip-address-01.htm").read()
re.findall('\d+\.\d+\.\d+\.\d+', page)

我不明白为什么上面写着:

文件“C:\Python33\lib\复制“,第201行,芬德尔 return\u compile(模式,标志).findall(字符串) TypeError:不能对类似于字节的对象使用字符串模式


Tags: 字符串importiprehttprequestwwwru
2条回答
import urllib
import re
page = urllib.urlopen("http://www.samair.ru/proxy/ip-address-01.htm").read()
print re.findall('\d+\.\d+\.\d+\.\d+', page)

工作并给了我结果:

^{pr2}$

编辑

  • 这适用于python2.7

读取urllib.request.urlopen返回的类文件对象的结果是一个bytes对象。您可以将其解码为unicode字符串并使用unicode正则表达式:

>>> re.findall('\d+\.\d+\.\d+\.\d+', page.decode('utf-8'))
['056.249.66.50', '100.44.124.8', '103.31.250.115', '105.236.180.243', '105.236.21.213', '108.171.162.172', '109.207.61.143', '109.207.61.197', '109.207.61.202', '109.226.199.129', '109.232.112.109', '109.236.220.98', '110.196.42.33', '110.74.197.141', '110.77.183.64', '110.77.199.111', '110.77.200.248', '110.77.219.154', '110.77.219.2', '110.77.221.208']

。。。或使用字节正则表达式:

^{pr2}$

取决于您希望使用的数据类型。在

相关问题 更多 >

    热门问题