link=''
path=r'E:/pydownload/1.txt'
f=open(path,'wb+')
for i in range(1,150):
print "it is download the %d page:" % i
new=link + str(i) + '.'+'shtml'
req=urllib2.Request(new)
fd=urllib2.urlopen(req)
soup=BeautifulSoup(fd.read(),from_encoding="utf-8"
ret=soup.find(id="content_1"
for r in ret.stripped_strings:
f.write(r)
f.close()
網(wǎng)頁(yè)的內(nèi)容是中文的。提示如下錯(cuò)誤:
Traceback (most recent call last):
File "E:\py_prj\test1.py", line 17, in <module>
f.write(r)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-7: ordinal not in range(12作者: ma__ 時(shí)間: 2015-12-12 00:53