为什么说,TypeError:期望一个字符缓冲区对象

时间:2013-04-28 20:26:14

标签: python-2.7

为什么会说,TypeError:期望一个字符缓冲区对象?

import urllib
import re

myfile = open("C:\Users\Teli\Desktop\pro.txt", "w")

page = urllib.urlopen("http://www.samair.ru/proxy/ip-address-01.htm").read()

a = re.findall('\d+\.\d+\.\d+\.\d+', page)

myfile.write(a)

myfile.close()

1 个答案:

答案 0 :(得分:2)

re.findall()返回匹配列表,而不是字符串。你必须从中获取字符串:

import urllib
import re

with open(r'C:\Users\Teli\Desktop\pro.txt', 'w') as handle:
    page = urllib.urlopen('http://www.samair.ru/proxy/ip-address-01.htm').read()
    addresses = re.findall(r'(\d+\.\d+\.\d+\.\d+)', page)

    handle.write('\n'.join(addresses))