Python爬取必应搜索首页图片

您所在的位置:网站首页 新法不溯及旧法 Python爬取必应搜索首页图片

Python爬取必应搜索首页图片

2023-02-11 08:44| 来源: 网络整理| 查看: 265

"""爬取每日必应首页图片:

必应首页图片一共七张"""

import requestsfrom bs4 import BeautifulSoupfrom requests.exceptions import RequestExceptionimport reimport os

def get_page(url): headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36' }

try: response = requests.get(url, headers=headers) if response.status_code == 200: return response.text return None except RequestException: return None

def download(url, path, fname): response = requests.get(url) if response: with open(os.path.join(path, fname), 'wb') as f: f.write(response.content) print('download: {} successful.'.format(fname)) else: print('faild: {}.'.format(fname))

for i in range(8): url = 'https://cn.bing.com/HPImageArchive.aspx?idx={}&n=1'.format(i) html = get_page(url) soup = BeautifulSoup(html, 'lxml') text = soup.find(name='url').string img_url = 'https://cn.bing.com' + text img_name = re.match('^/th\?id=(.*?)&', text).group(1) download(img_url, 'E:/wallpaper/bing', img_name)



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3