在我的本地Windows机器上,我如何编写脚本每天下载漫画并通过电子邮件发送给自己?

时间:2021-05-03 07:12:41

on my local Windows machine, how do i write a script to download a comic strip every day and email it to myself?

在我的本地Windows机器上,我如何编写脚本每天下载漫画并通过电子邮件发送给自己?

such as
http://comics.com/peanuts/

例如http://comics.com/peanuts/

Update: i know how to download the image as a file. the hard part is how to email it from my local Windows machine.

更新:我知道如何将图像下载为文件。困难的部分是如何从我的本地Windows机器发送电子邮件。

6 个解决方案

#1


1  

Emailing it is easy. Pick a library in your favorite language and read the documentation. Send it through your regular email account, or create a new free GMail account for it.

通过电子邮件发送很容易。选择您喜欢的语言库并阅读文档。通过您的常规电子邮件帐户发送,或为其创建新的免费GMail帐户。

Sometimes attachments can indeed be tricky, though. If nothing else, give it a good whirl with whatever library you like most, and post another specific question about any problems you encounter.

但有时附件确实很棘手。如果没有别的,请用你最喜欢的任何库给它一个很好的旋转,并发布关于你遇到的任何问题的另一个具体问题。

#2


8  

This depends how precise you want to be. Downloading the entire web page wouldn't be too challenging - using wget, as Earwicker mentions above.

这取决于你想要的精确程度。下载整个网页不会太具挑战性 - 使用wget,正如Earwicker在上面提到的那样。

If you want the actual image file of the comic downloaded, you would need a bit more in your arsenal. In Python - because that's what I know best - I would imagine you'd need to use urllib to access the page, and then a regular expression to identify the correct part of the page. Therefore you will need to know the exact layout of the page and the absolute URL of the image.

如果你想下载漫画的实际图像文件,你需要在你的武器库中多一点。在Python中 - 因为这是我最熟悉的 - 我想你需要使用urllib来访问页面,然后使用正则表达式来识别页面的正确部分。因此,您需要知道页面的确切布局和图像的绝对URL。

For XKCD, for example, the following works:

例如,对于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.'

You can then email it however you feel comfortable.

然后您可以通过电子邮件发送,但感觉很舒服

#3


3  

A quick look on google reveals two command-line programs that you should be able to lash together in a batch file or using the scripting language of your choice.

快速浏览一下谷歌会发现两个命令行程序,你应该能够在批处理文件中或使用你选择的脚本语言。

http://www.gnu.org/software/wget/ - to do the download

http://www.gnu.org/software/wget/ - 进行下载

http://www.beyondlogic.org/solutions/cmdlinemail/cmdlinemail.htm - to send the email

http://www.beyondlogic.org/solutions/cmdlinemail/cmdlinemail.htm - 发送电子邮件

You can use the Windows Task Scheduler in control panel to make it run daily.

您可以使用控制面板中的Windows任务计划程序使其每天运行。

If you are using Python there are surely going to be convenient libraries to do the downloading/emailing parts - browse the official Python site.

如果你使用的是Python,肯定会有方便的库来下载/发送电子邮件 - 浏览官方的Python网站。

#4


2  

Configure feedburner on the RSS feed, subscribe yourself to the email alerts?

在RSS源上配置feedburner,订阅自己的电子邮件警报?

#5


1  

Here is perhaps the shortest distance to your goal.

这可能是到达目标的最短距离。

It's not simple... you will need to work out how to parse out the image, and the peanuts example seems to be an unpredictable URI, so it might be more difficult than it looks to get the image itself. Your best bet will be to read the HTML of the remote webpage, write a regex to parse out the image url. Then the mail function will work fine, send an HTML email by setting the headers in the mail() function to something like:

这并不简单......你需要弄清楚如何解析图像,而花生的例子似乎是一个不可预测的URI,所以它可能比看起来更难以获得图像本身。您最好的选择是阅读远程网页的HTML,编写正则表达式来解析图像网址。然后邮件功能将正常工作,通过将mail()函数中的标题设置为以下内容来发送HTML电子邮件:

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html;";
$headers .= " charset=iso-8859-1\r\n";

With the image tags in the mail. This will let you receive emails with all your comic strips placed one after another. Your email software will do the HTTP requests to download the images for you, so you can avoid having to attach the images directly.

使用邮件中的图像标签。这将让您收到一个接一个地放置所有漫画的电子邮件。您的电子邮件软件将执行HTTP请求以下载图像,因此您可以避免直接附加图像。

#6


1  

It's pretty simple if you already know how to download the file. Once its downloaded create a cronjob that emails it to yourself.

如果您已经知道如何下载文件,这很简单。下载后,创建一个通过电子邮件发送给自己的cronjob。

Using something like phpmailer would be the easiest way to email it

使用像phpmailer这样的东西是发送电子邮件的最简单方法

http://phpmailer.codeworxtech.com/index.php?pg=examplebmail

http://phpmailer.codeworxtech.com/index.php?pg=examplebmail

#1


1  

Emailing it is easy. Pick a library in your favorite language and read the documentation. Send it through your regular email account, or create a new free GMail account for it.

通过电子邮件发送很容易。选择您喜欢的语言库并阅读文档。通过您的常规电子邮件帐户发送,或为其创建新的免费GMail帐户。

Sometimes attachments can indeed be tricky, though. If nothing else, give it a good whirl with whatever library you like most, and post another specific question about any problems you encounter.

但有时附件确实很棘手。如果没有别的,请用你最喜欢的任何库给它一个很好的旋转,并发布关于你遇到的任何问题的另一个具体问题。

#2


8  

This depends how precise you want to be. Downloading the entire web page wouldn't be too challenging - using wget, as Earwicker mentions above.

这取决于你想要的精确程度。下载整个网页不会太具挑战性 - 使用wget,正如Earwicker在上面提到的那样。

If you want the actual image file of the comic downloaded, you would need a bit more in your arsenal. In Python - because that's what I know best - I would imagine you'd need to use urllib to access the page, and then a regular expression to identify the correct part of the page. Therefore you will need to know the exact layout of the page and the absolute URL of the image.

如果你想下载漫画的实际图像文件,你需要在你的武器库中多一点。在Python中 - 因为这是我最熟悉的 - 我想你需要使用urllib来访问页面,然后使用正则表达式来识别页面的正确部分。因此,您需要知道页面的确切布局和图像的绝对URL。

For XKCD, for example, the following works:

例如,对于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.'

You can then email it however you feel comfortable.

然后您可以通过电子邮件发送,但感觉很舒服

#3


3  

A quick look on google reveals two command-line programs that you should be able to lash together in a batch file or using the scripting language of your choice.

快速浏览一下谷歌会发现两个命令行程序,你应该能够在批处理文件中或使用你选择的脚本语言。

http://www.gnu.org/software/wget/ - to do the download

http://www.gnu.org/software/wget/ - 进行下载

http://www.beyondlogic.org/solutions/cmdlinemail/cmdlinemail.htm - to send the email

http://www.beyondlogic.org/solutions/cmdlinemail/cmdlinemail.htm - 发送电子邮件

You can use the Windows Task Scheduler in control panel to make it run daily.

您可以使用控制面板中的Windows任务计划程序使其每天运行。

If you are using Python there are surely going to be convenient libraries to do the downloading/emailing parts - browse the official Python site.

如果你使用的是Python,肯定会有方便的库来下载/发送电子邮件 - 浏览官方的Python网站。

#4


2  

Configure feedburner on the RSS feed, subscribe yourself to the email alerts?

在RSS源上配置feedburner,订阅自己的电子邮件警报?

#5


1  

Here is perhaps the shortest distance to your goal.

这可能是到达目标的最短距离。

It's not simple... you will need to work out how to parse out the image, and the peanuts example seems to be an unpredictable URI, so it might be more difficult than it looks to get the image itself. Your best bet will be to read the HTML of the remote webpage, write a regex to parse out the image url. Then the mail function will work fine, send an HTML email by setting the headers in the mail() function to something like:

这并不简单......你需要弄清楚如何解析图像,而花生的例子似乎是一个不可预测的URI,所以它可能比看起来更难以获得图像本身。您最好的选择是阅读远程网页的HTML,编写正则表达式来解析图像网址。然后邮件功能将正常工作,通过将mail()函数中的标题设置为以下内容来发送HTML电子邮件:

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html;";
$headers .= " charset=iso-8859-1\r\n";

With the image tags in the mail. This will let you receive emails with all your comic strips placed one after another. Your email software will do the HTTP requests to download the images for you, so you can avoid having to attach the images directly.

使用邮件中的图像标签。这将让您收到一个接一个地放置所有漫画的电子邮件。您的电子邮件软件将执行HTTP请求以下载图像,因此您可以避免直接附加图像。

#6


1  

It's pretty simple if you already know how to download the file. Once its downloaded create a cronjob that emails it to yourself.

如果您已经知道如何下载文件,这很简单。下载后,创建一个通过电子邮件发送给自己的cronjob。

Using something like phpmailer would be the easiest way to email it

使用像phpmailer这样的东西是发送电子邮件的最简单方法

http://phpmailer.codeworxtech.com/index.php?pg=examplebmail

http://phpmailer.codeworxtech.com/index.php?pg=examplebmail