我这里有一个代码,通过查找regex的文件扩展名来查找所有使用regex的图像文件。现在我要做的是将它保存到我计算机上的指定路径,并保留其原始文件名。我当前的代码找到了这些图像,因为我通过打印源文件来测试,但没有将其保存到指定的目录中-也许任何人都可以帮助我调整代码。在
提前谢谢。在
我的代码是:
import urllib,re,os
_in = raw_input('< Press enter to download images from first page >')
if not os.path.exists('FailImages'): # Directory that I want to save the image to
os.mkdir('FailImages') # If no directory create it
source = urllib.urlopen('http://www.samplewebpage.com/index.html').read()
imgs = re.findall('\w+.jpg',source) # regex finds files with .jpg extension
这一点需要调整
^{pr2}$
这应该能让你走了。它不处理是否是外部链接,但它会抓取本地图像
可选
$ sudo easy_install requests
如果使用请求,取消对3
f.____
行的注释和最后一行的注释:注意:Requests效果更好,可以确保发送正确的头,urllib在大多数情况下可能不起作用。在
相关问题 更多 >
编程相关推荐