爬虫的自我修养课后题第三道
import urllib.requestcount=0
with open('urls.txt')as f:
for each in f:
count+=1
response=urllib.request.urlopen(each).read()
name='url_'+str(count)+'.txt'
with open(name,'w') as fn:
fn.write(str(response))
这是自己写的代码,没有把编码考虑进去
import urllib.request
import chardet
def main():
i = 0
with open("url.txt", "r") as f:
# 读取待访问的网址
# 由于urls.txt每一行一个URL
# 所以按换行符'\n'分割
urls = f.read().splitlines()
for each_url in urls:
response = urllib.request.urlopen(each_url)
html = response.read()
# 识别网页编码
encode = chardet.detect(html)['encoding']
if encode == 'GB2312':
encode = 'GBK'
i += 1
filename = "url_%d.txt" % i
with open(filename, "w", encoding=encode) as each_file:
each_file.write(html.decode(encode, "ignore"))
if __name__ == "__main__":
main()
这是答案,然后都能输出,但我自己写的比答案少一些内容,是因为没有考虑编码的问题嘛?求大神指点 是的 qiuyouzhi 发表于 2020-6-23 15:10
是的
好的谢谢
页:
[1]