Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bing壁纸捕捉器与python_Python_Debian - Fatal编程技术网

Bing壁纸捕捉器与python

Bing壁纸捕捉器与python,python,debian,Python,Debian,我在这里找到了以下代码: 很抱歉,我不能在这里发表评论,所以我在这里询问。但是我有一些问题。 当我在Debian中运行它时,它说“列表索引超出范围”,我不知道如何解决这个问题。我很抱歉,如果这听起来很愚蠢的话 #!/usr/bin/python3 from bs4 import BeautifulSoup import os import urllib from urllib.request import urlopen BingXML_URL ="http://www.b

我在这里找到了以下代码:

很抱歉,我不能在这里发表评论,所以我在这里询问。但是我有一些问题。 当我在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)

您使用的链接与文章中所述的链接不同。文章说链接是

,但您使用了

Bing对此没有回应。试试这个新链接,看看它是否有效

此外,还有一些小错误:

  • urlretrieve
    位于
    urllib.request
    中,而不是
    urllib
    ,因此从urllib.request导入urlopen、urlretrieve和
    urlretrieve(ImageURL,ImageName)
    可能更好
  • Images[0]。startdate
    是一个
    标记
    对象,无法将其添加到
    str
    “.jpg”
    。您可以使用
    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)
    

    非常感谢你。是的,我在那里犯了错误。我还可以问一下如何更改图像的路径吗?我想将它保存在我的图片中,然后自动显示在桌面背景上。很抱歉问了这么多问题。只需更改
    urlretrieve(ImageURL,ImageName)
    ImageName
    参数实际上是文件路径。例如,将其更改为
    ”/home/yourname/Pictures/'+ImageName
    将保存在图片文件夹下。谢谢。我真的很感激。如何使用os.path.expanduser(“~”)以更改其他用户的路径?您可以使用
    os.path.expanduser(“~/Pictures/”+ImageName)
    将图像保存到当前用户的图片文件夹中。