-1

为什么会说 TypeError: expected a character buffer object?

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()
4

1 回答 1

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))
于 2013-04-28T20:29:33.273 回答