我想编写一个抓取bing.com壁纸并保存它的python脚本。
这些壁纸的网址如下:
http://www.bing.com/az/hprichbg/rb/EuropeESA_DE-DE7849418832_1920x1080.jpg
http://www.bing.com/az/hprichbg/rb/CanisLupus_DE-DE11366975292_1920x1080.jpg
http://www.bing.com/az/hprichbg/rb/HouseBoats_DE-DE8695714746_1920x1080.jpg
有没有办法自动找到今日壁纸的图片网址?
答案 0 :(得分:1)
使用以下代码抓取并将其保存在文件夹中:
import datetime
from urllib.request import urlopen, urlretrieve
from xml.dom import minidom
import os
import sys
def join_path(*args):
# Takes an list of values or multiple values and returns an valid path.
if isinstance(args[0], list):
path_list = args[0]
else:
path_list = args
val = [str(v).strip(' ') for v in path_list]
return os.path.normpath('/'.join(val))
dir_path = os.path.dirname(os.path.realpath(__file__))
save_dir = join_path(dir_path, 'images')
if not os.path.exists(save_dir):
os.makedirs(save_dir)
def set_wallpaper(pic_path):
if sys.platform.startswith('win32'):
cmd = 'REG ADD \"HKCU\Control Panel\Desktop\" /v Wallpaper /t REG_SZ /d \"%s\" /f' %pic_path
os.system(cmd)
os.system('rundll32.exe user32.dll, UpdatePerUserSystemParameters')
print('Wallpaper is set.')
elif sys.platform.startswith('linux'):
os.system(''.join(['gsettings set org.gnome.desktop.background picture-uri file://', pic_path]))
print('Wallpaper is set.')
else:
print('OS not supported.')
return
return
def download_old_wallpapers(minus_days=False):
"""Uses download_wallpaper(set_wallpaper=False) to download the last 20 wallpapers.
If minus_days is given an integer a specific day in the past will be downloaded.
"""
if minus_days:
download_wallpaper(idx=minus_days, use_wallpaper=False)
return
for i in range(0, 20): # max 20
download_wallpaper(idx=i, use_wallpaper=False)
def download_wallpaper(idx=0, use_wallpaper=True):
# Getting the XML File
try:
usock = urlopen(''.join(['http://www.bing.com/HPImageArchive.aspx?format=xml&idx=',
str(idx), '&n=1&mkt=ru-RU'])) # ru-RU, because they always have 1920x1200 resolution
except Exception as e:
print('Error while downloading #', idx, e)
return
try:
xmldoc = minidom.parse(usock)
# This is raised when there is trouble finding the image url.
except Exception as e:
print('Error while processing XML index #', idx, e)
return
# Parsing the XML File
for element in xmldoc.getElementsByTagName('url'):
url = 'http://www.bing.com' + element.firstChild.nodeValue
# Get Current Date as fileName for the downloaded Picture
now = datetime.datetime.now()
date = now - datetime.timedelta(days=int(idx))
pic_path = join_path(save_dir, ''.join([date.strftime('bing_wp_%d-%m-%Y'), '.jpg']))
if os.path.isfile(pic_path):
print('Image of', date.strftime('%d-%m-%Y'), 'already downloaded.')
if use_wallpaper:
set_wallpaper(pic_path)
return
print('Downloading: ', date.strftime('%d-%m-%Y'), 'index #', idx)
# Download and Save the Picture
# Get a higher resolution by replacing the file name
urlretrieve(url.replace('_1366x768', '_1920x1200'), pic_path)
# Set Wallpaper if wanted by user
if use_wallpaper:
set_wallpaper(pic_path)
if __name__ == "__main__":
download_wallpaper()
答案 1 :(得分:0)
#include <iostream>
#include <vector>
#include <bitset>
#include <stdio.h>
using namespace std;
ostream& operator<< ( ostream& os, const vector<uint32_t>& v )
{
for( auto it = v.begin(); it != v.end(); it++ )
{
os << *it << "\n";
}
return os;
}
inline bitset<15> make_pattern()
{
char field;
bitset<15> pattern;
for( uint32_t offset = 0; offset < 3; offset++ )
{
cin >> field;
if( field == 'x' ) pattern.set(offset);
cin >> field;
if( field == 'x' ) pattern.set(offset + 5);
cin >> field;
if( field == 'x' ) pattern.set(offset + 10);
}
return pattern;
}
int main()
{
uint32_t n, m, p;
cin >> n;
cin >> p;
cin >> m;
vector<bool> forbidden_patterns( 33000 );
for( uint32_t i = 0; i < p; i++ )
{
auto pattern = make_pattern();
forbidden_patterns[pattern.to_ulong()] = true; // true := forbidden; false := allowed.
forbidden_patterns[(pattern << 1).to_ulong()] = true;
forbidden_patterns[(pattern << 2).to_ulong()] = true;
//cout << forbidden_patterns[(pattern<<3).to_ulong()];
//cout << pattern.to_ulong();
}
for( uint32_t i = 0; i< 33000; i++ ) //checking the contents of the forbidden_patterns vector
{
bitset<15> bs = i;
if(forbidden_patterns[i] == true)
cout << bs << "\n";
}
cout << "\n\n";
//bitmasks for setting 2 rows of a 3 column pattern to 0;
bitset<15> bottom_rows_reset_mask;
bottom_rows_reset_mask.set(3); bottom_rows_reset_mask.set(8); bottom_rows_reset_mask.set(13);
bottom_rows_reset_mask.set(4); bottom_rows_reset_mask.set(9); bottom_rows_reset_mask.set(14);
bottom_rows_reset_mask = ~bottom_rows_reset_mask;
bitset<15> top_rows_reset_mask;
top_rows_reset_mask.set(0); top_rows_reset_mask.set(5); top_rows_reset_mask.set(10);
top_rows_reset_mask.set(1); top_rows_reset_mask.set(6); top_rows_reset_mask.set(11);
top_rows_reset_mask = ~top_rows_reset_mask;
bitset<15> top_and_bottom_reset_mask;
top_and_bottom_reset_mask.set(0); top_and_bottom_reset_mask.set(5); top_and_bottom_reset_mask.set(10);
top_and_bottom_reset_mask.set(4); top_and_bottom_reset_mask.set(9); top_and_bottom_reset_mask.set(14);
top_and_bottom_reset_mask = ~top_and_bottom_reset_mask;
vector<uint32_t> left( 1024, 1 );
vector<uint32_t> right( 1024, 0 );
for( uint32_t column = 3; column <= n; column++ )
{
for( uint32_t first_2_columns = 0; first_2_columns < 1024; first_2_columns++ )
{
if( left[first_2_columns] == 0 ) continue;
for( uint32_t third_column = 0; third_column < 32; third_column++ )
{
bitset<15> t_patt = (first_2_columns | third_column << 10) & top_and_bottom_reset_mask.to_ulong();
//cout << t_patt << "\n"; //getchar();
if( forbidden_patterns[t_patt.to_ulong()] == true )
{
//cout << t_patt << "\n"; getchar();
continue;
}
t_patt = (first_2_columns | third_column << 10) & bottom_rows_reset_mask.to_ulong();
//cout << t_patt << "\n"; //getchar();
if( forbidden_patterns[t_patt.to_ulong()] == true )
{
//cout << t_patt << "\n"; getchar();
continue;
}
t_patt = (first_2_columns | third_column << 10) & top_rows_reset_mask.to_ulong();
//cout << t_patt << "\n"; //getchar();
if( forbidden_patterns[t_patt.to_ulong()] == true )
{
//cout << t_patt << "\n"; getchar();
continue;
}
t_patt = first_2_columns | third_column << 10;
//cout << t_patt << "\n";
auto next_2_column_pattern = (t_patt >> 5).to_ulong();
//t_patt = next_2_column_pattern; cout << t_patt << "\n"; getchar();
right[next_2_column_pattern] = (right[next_2_column_pattern] + left[first_2_columns]) % m;
}
}
left.swap(right);
right.assign(1024, 0u);
}
uint32_t sum = 0;
for( int i = 0; i < 1024; i++ )
sum = (sum + left[i]) % m;
cout << sum;
}
答案 2 :(得分:0)
根据此related SO question中的一些有用答案,这里有一个简单的Python脚本来获取当天的Bing照片:
TurtleTears_EN-US7942276596_1920x1080.jpg
这会将{forum.length > 0 ?
forum.map( ( {title, id} ) => {
return <Picker.Item key={id} label={title} value={id} />
})
:
null
}
等文件写入执行脚本的同一目录。当然,可以在这里调整一大堆东西,但可以很容易地完成工作。