我开发了一个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”的目录下。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句