Python download homepage picture

The following is a downloader program that uses RoboBrowser and Python to download homepage images, and uses https://www.duoip.cn/get _proxy to obtain the proxy:

import os
import time
from robobrowser import RoboBrowser
import requests
​
def get_proxy():
    url = "https://www.duoip.cn/get_proxy"
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
    response = requests.get(url, headers=headers)
    if response.status_code == 200:
        return response.text
    else:
        return None
​
def download_image(proxy):
    browser = RoboBrowser(history=True, user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3")
    proxy_dict = {"http": proxy, "https": proxy}
    browser.set_proxies(proxy_dict)
​
    url = "https://www.jd.com"
    browser.open(url)
    time.sleep(5)
​
    # 获取首页图片的URL
    img_url = browser.find_all('img', {'class': 'lazy-image'})[0].get('data-original')
​
    # 下载图片
    response = requests.get(img_url, stream=True)
    if response.status_code == 200:
        image_content = response.content
        image_filename = os.path.join(os.getcwd(), "jd_image.jpg")
        with open(image_filename, "wb") as image_file:
            image_file.write(image_content)
        print("图片下载完成,保存在:", image_filename)
    else:
        print("图片下载失败")
​
if __name__ == "__main__":
    proxy = get_proxy()
    if proxy:
        download_image(proxy)
    else:
        print("无法获取代理")

This program first obtains a proxy, then uses RoboBrowser to open the homepage of jd.com, obtains the URL of the homepage image, and downloads it locally. Please note that this program requires Python 3.6 and above to run.

Guess you like

Origin blog.csdn.net/weixin_73725158/article/details/134003373