File "news01/news01.py", line 12, in <module> req = urllib.request.Request(url) NameError: name 'urllib' is not defined Traceback (most recent call last): File "news01/news01_test.py", line 25, in <module> f=open("news1.txt","rb") FileNotFoundError: [Errno 2] No such file or directory: 'news1.txt'
时间: 2024-01-03 19:05:34 浏览: 33
这两个错误的原因是不同的。
第一个错误是`NameError: name 'urllib' is not defined`,它意味着Python无法识别`urllib`模块。这可能是因为您没有导入`urllib`模块。您可以在代码的开头添加以下行来导入它:
```
import urllib.request
```
第二个错误是`FileNotFoundError: [Errno 2] No such file or directory: 'news1.txt'`,它意味着Python找不到名为`news1.txt`的文件。这可能是因为您尚未创建该文件,或者文件名拼写错误。请确保您已经创建了名为`news1.txt`的文件,并且文件名拼写正确。
希望这可以帮助您解决这些错误!
相关问题
Traceback (most recent call last): File "D:\PythonProject\venv\pq.py", line 46, in <module> getVideo(i) File "D:\PythonProject\venv\pq.py", line 36, in getVideo req = urllib.request.Request("https://v.qq.com/x/cover/mzc00200mhcv61p/c0046jmqwhk.html" %page) TypeError: not all arguments converted during string formatting
这个错误的原因是在第36行的代码中,你使用了字符串格式化操作符 `%`,但是你没有传递足够的参数进行格式化。具体来说,你需要将字符串中的格式化占位符 `%s` 替换为实际的参数值,例如:
```
req = urllib.request.Request("https://v.qq.com/x/cover/mzc00200mhcv61p/c0046jmqwhk.html" % i)
```
这将会将 `%s` 替换为变量 `i` 的值,从而避免这个错误。
运行下面代码,运行结果没有保存文件,请帮我找出原因 # -- coding: utf-8 -- # 指定文件编码格式为utf-8 import urllib.request import re def getNovertContent(): url = 'http://www.quannovel.com/read/640/' req = urllib.request.Request(url) req.add_header( 'User-Agent', ' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36') data = urllib.request.urlopen(req).read().decode('gbk') str1 = str(data) # 将网页数据转换为字符串 reg = r'<li><a href="(.?)" title=".?">(.?)</a><li>' reg = re.compile(reg) urls = reg.findall(str1) for url in urls: novel_url = url[0] novel_title = url[1] chapt = urllib.request.urlopen(novel_url).read() chapt_html = chapt.decode('gbk') reg = r'</script> (.?)</script> type="text/javascript">' reg = re.compile(reg, re.S) chapt_content = reg.findall(reg, chapt_html) chapt_content = chapt_content[0].replace( " ", "") chapt_content = chapt_content.replace("<br />", "") print("正在保存 %s" % novel_title) with open("{}.txt".format(novel_title), 'w') as f: f.write(chapt_content) getNovertContent()
代码中存在两个问题导致没有保存文件:
1. 在正则表达式匹配章节内容时,应该调用 `reg.findall(chapt_html)` 而不是 `reg.findall(reg, chapt_html)`。
2. 在打开文件时,需要指定文件的编码格式为 utf-8,因为在代码开头已经指定了文件编码格式为 utf-8。可以将 `with open("{}.txt".format(novel_title), 'w') as f:` 改为 `with open("{}.txt".format(novel_title), 'w', encoding='utf-8') as f:`。
修改后的代码如下:
```python
# -- coding: utf-8 --
# 指定文件编码格式为utf-8
import urllib.request
import re
def getNovertContent():
url = 'http://www.quannovel.com/read/640/'
req = urllib.request.Request(url)
req.add_header('User-Agent', ' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36')
data = urllib.request.urlopen(req).read().decode('gbk')
str1 = str(data) # 将网页数据转换为字符串
reg = r'<li><a href="(.+?)" title=".+?">(.+?)</a><li>'
reg = re.compile(reg)
urls = reg.findall(str1)
for url in urls:
novel_url = url[0]
novel_title = url[1]
chapt = urllib.request.urlopen(novel_url).read()
chapt_html = chapt.decode('gbk')
reg = r'</script> (.+?)</script> type="text/javascript">'
reg = re.compile(reg, re.S)
chapt_content = reg.findall(chapt_html)
chapt_content = chapt_content[0].replace(" ", "")
chapt_content = chapt_content.replace("<br />", "")
print("正在保存 %s" % novel_title)
with open("{}.txt".format(novel_title), 'w', encoding='utf-8') as f:
f.write(chapt_content)
getNovertContent()
```