|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
问题描述:
GBK网页保存下来后储存为txt过程中,转码为GB2312则txt打开不是中文,不转码则保存到一半报错,但是txt内容为中文。
跪求各位大神帮忙指点,谢谢!
import requests
from bs4 import BeautifulSoup
f = open('小说.txt','w')
for i in range(1, 35):
url = 'http://www.xs99.cc/data/yanqing2/qUsiV31908/book31908_' + str(i) + '.html'
response = requests.get(url)
html = response.content.decode('GBK')
soup = BeautifulSoup(html,'html.parser')
body = soup.body
content = body.find('div',attrs={'id':'content'}).get_text() #不注释则会报错,注释则TXT打开后不是中文#.encode('gb2312','ignore')
print('第' + str(i) + '页',file=f)
print(content[120:],file=f)
f = open('小说.txt','w',encoding='utf-8')
即可
|
|