Bing壁纸捕捉器与python

2024-10-04 01:34:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我在这里找到了这个代码:Is there a way to get Bing's photo of the day?

很抱歉,我不能在这里发表评论,所以我在这里询问。但是我有一些问题。 当我在Debian中运行它时,它说“列表索引超出范围”,我不知道如何解决这个问题。我很抱歉,如果这听起来很愚蠢的话

    #!/usr/bin/python3

from bs4 import BeautifulSoup
import os
import urllib
from urllib.request import urlopen

BingXML_URL ="http://www.bing.com/HPImageArchive.aspx?"
page= urlopen(BingXML_URL).read()
BingXml =BeautifulSoup(page, "lxml")
Images = BingXml.find_all('image')
ImageURL ="https://www.bing.com" + Images[0].url.text
ImageName = Images[0].startdate + ".jpg"

urllib.urlretrieve(ImageURL, ImageName)

Tags: fromimportcomurlwwwpageurlliburlopen
1条回答
网友
1楼 · 发布于 2024-10-04 01:34:34

您使用的链接与文章中所述的链接不同。文章说链接是
https://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1&mkt=en-US
,但您使用了
http://www.bing.com/HPImageArchive.aspx?
Bing对此没有回应。试试这个新链接,看看它是否有效

此外,还有一些小错误:

  1. urlretrieve在{}中,而不是{},因此使用{}和{}可能更好
  2. Images[0].startdateTag对象,无法将其添加到str{}中。您可以使用Images[0].startdate.get_text()获取字符串并使用str进行添加

因此,完成的代码应该是:

#!/usr/bin/python3
from bs4 import BeautifulSoup
import os
import urllib
from urllib.request import urlopen, urlretrieve

BingXML_URL ="http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1&mkt=en-US"
page= urlopen(BingXML_URL).read()
BingXml =BeautifulSoup(page, "lxml")
Images = BingXml.find_all('image')
ImageURL ="https://www.bing.com" + Images[0].url.text
ImageName = Images[0].startdate.get_text() + ".jpg"

urlretrieve(ImageURL, ImageName)

相关问题 更多 >