以下是一个使用RoboBrowser和Python下载首页图片的下载器程序,并使用https://www.duoip.cn/get_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("无法获取代理")
这个程序首先获取一个代理,然后使用RoboBrowser打开jd.com的首页,获取首页图片的URL,并将其下载到本地。请注意,这个程序需要在Python3.6及更高版本下运行。