[英]Unable to print the files with special characters while using python
我開發了一個Web搜尋器,以提取Wiki鏈接中的所有源代碼。 寫入一些文件后,程序終止。
def fetch_code(link_list):
for href in link_list:
response = urllib2.urlopen("https://www.wikipedia.org/"+href)
content = response.read()
page = open("%s.html" % href, 'w')
page.write(content.replace("[\/:?*<>|]", " "))
page.close()
link_list
是一個數組,具有從種子頁面提取的鏈接。
我執行后得到的錯誤是
IOError: [Errno 2] No such file or directory: u'M/s.html'
您不能創建名稱中帶有“ /”的文件。
您可以將文件名轉義為M%2Fs.html
/是%2F
在python2中,您可以簡單地使用urllib來轉義文件名,例如:
import urllib
filePath = urllib.quote_plus('M/s.html')
print(filePath)
另一方面,您也可以將http響應保存到層次結構中,例如,M / s.html意味着s.html文件位於名為“ M”的目錄下。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.