|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
小甲鱼的答案:
- import urllib.request
- import chardet
- def main():
- i = 0
-
- with open("urls.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()
复制代码
运行后报错:
- =========== RESTART: /Users/cristikaQ/Documents/Pycode/53-2小甲鱼.py ===========
- Traceback (most recent call last):
- File "/Users/cristikaQ/Documents/Pycode/53-2小甲鱼.py", line 29, in <module>
- main()
- File "/Users/cristikaQ/Documents/Pycode/53-2小甲鱼.py", line 26, in main
- each_file.write(html.decode(encode, "ignore"))
- TypeError: decode() argument 1 must be str, not None
- >>>
复制代码
报错说decode()的第一个参数必须是字符串,可是encode是字符串没错啊
我自己写的也是同样的问题
求助 |
|