在我的本地 Windows 机器上,我如何编写一个脚本来每天下载漫画并将其通过电子邮件发送给自己?
更新:我知道如何将图像下载为文件。困难的部分是如何从我的本地 Windows 计算机通过电子邮件发送它。
最佳答案
这取决于您想要的精确度。下载整个网页不会太具有挑战性 - 使用 wget,正如 Earwicker 上面提到的那样。
如果您想下载漫画的实际图像文件,您的武器库中需要更多。在 Python 中——因为这是我最了解的——我想你需要使用 urllib 来访问页面,然后使用正则表达式来识别页面的正确部分。因此,您需要知道页面的确切布局和图像的绝对 URL。
例如,对于 XKCD,以下工作:
#!/usr/bin/env python
import re, urllib
root_url = 'http://xkcd.com/'
img_url = r'http://imgs.xkcd.com/comics/'
dl_dir = '/path/to/download/directory/'
# Open the page URL and identify comic image URL
page = urllib.urlopen(root_url).read()
comic = re.match(r'%s[\w]+?\.(png|jpg)' % img_url, page)
# Generate the filename
fname = re.sub(img_url, '', comic)
# Download the image to the specified download directory
try:
image = urllib.urlretrieve(comic, '%s%s' % (dl_dir, fname))
except ContentTooShortError:
print 'Download interrupted.'
else:
print 'Download successful.'
然后您可以根据自己的喜好通过电子邮件发送。
关于php - 在我的本地 Windows 机器上,我如何编写一个脚本来每天下载漫画并将其通过电子邮件发送给自己?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/909618/