图片下载 Parameters: path - str 图片保存地址 image_url - str 图片下载地址 Returns: None
(path, image_url)
| 156 | return image_urls |
| 157 | |
| 158 | def download_image(path, image_url): |
| 159 | """ |
| 160 | 图片下载 |
| 161 | Parameters: |
| 162 | path - str 图片保存地址 |
| 163 | image_url - str 图片下载地址 |
| 164 | Returns: |
| 165 | None |
| 166 | """ |
| 167 | print(image_url) |
| 168 | filename = image_url.split('/')[-1] |
| 169 | image_path = os.path.join(path, filename) |
| 170 | download_headers = {'Accept': '*/*', |
| 171 | 'Accept-Encoding': 'gzip, deflate, br', |
| 172 | 'Accept-Language': 'zh-CN,zh;q=0.9', |
| 173 | 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36'} |
| 174 | size = 0 |
| 175 | with closing(requests.get(image_url, headers=download_headers, stream=True)) as response: |
| 176 | chunk_size = 1024 |
| 177 | content_size = int(response.headers['content-length']) |
| 178 | if response.status_code == 200: |
| 179 | sys.stdout.write(filename+'下载中:\n') |
| 180 | sys.stdout.write(' [文件大小]:%0.2f MB\n' % (content_size / chunk_size / 1024)) |
| 181 | |
| 182 | with open(image_path, 'wb') as file: |
| 183 | for data in response.iter_content(chunk_size = chunk_size): |
| 184 | file.write(data) |
| 185 | size += len(data) |
| 186 | file.flush() |
| 187 | sys.stdout.write(' [下载进度]:%.2f%%' % float(size / content_size * 100) + '\r') |
| 188 | sys.stdout.flush() |
| 189 | |
| 190 | def run(path, keyword, num): |
| 191 | """ |