import requests
from bs4 import BeautifulSoup
url = input("URL:")
grab_page = requests.get(url)
parse_page = BeautifulSoup(grab_page.text, "html.parser")
file_name = parse_page.title.string.replace("\\,()", "")
newfile = open(file_name + ".html", "w+")
newfile.write(grab_page.text)
当我尝试运行上面的代码时,使用this particular URL,,其中网页的标题是“如何安装JDK 8”(在Windows上, Mac OS,Ubuntu)和Java编程入门“我收到了以下错误:
Traceback (most recent call last):
File "C:/Users/LKT/PycharmProjects/webpagegrabber/main.py", line 12, in <module>
newfile = open(file_name + ".html", "w+")
OSError: [Errno 22] Invalid argument: 'How to Install JDK 8 (on Windows,\r\nMac OS, Ubuntu)
and Get Started with Java Programming.html'
我哪里出错了?
答案 0 :(得分:2)
您的文件名包含无效字符('\ n','\ r')。所以你不能在Windows中创建这样的文件。如windows developer center:
中所述整数表示的范围为1的字符 到31,除了这些字符的备用数据流 被允许。有关文件流的更多信息,请参阅文件 流。