[英]how to pass input in argparse argument in python
this code should ask 2 optional and 1 required argument, 'format' and 'type' argument are optional and 'url' is required argument which will be the address of website from where you wants to download image(either jpg or/and .png type image,depend on what value you give in 'type' argument ) and save it as either as individual image or single JSON file(depend on what value you give in 'format' argument ), i am getting error can you tell where i am going wrong,here is my code.. 此代码应询问2个可选参数和1个必需参数,'format'和'type'参数是可选参数,而'url'是必需参数,它将是您要下载图像的网站的地址(jpg或/和.png)键入图像,取决于您在'type'参数中提供的值,然后将其另存为单个图像或单个JSON文件(取决于您在'format'参数中提供的值),我得到了错误提示,您能知道我在哪里出问题了,这是我的代码。
import argparse
import base64
import json
import os
from bs4 import BeautifulSoup
import requests
def scrape(url,format_,type_):
try:
page = requests.get(url)
except requests.RequestException as rex:
print(str(rex))
else:
soup = BeautifulSoup(url,'html.parser')
images = _fetch_images(soup,url)
images = _filter_images(images,type_)
_save(images,format_)
def _fetch_images(soup,base_url):
images = []
for img in soup.find_all('img'):
src = img.get('src')
img_url = ('{base_url}/{src}'.format(base_url,src))
name = img_url.split('\\')[-1]
images.append(dict(name=name,url=img_url))
return images
def _filter_images(images,type_):
if type == 'all':
return images
ext_map = {'png':['.png'],'jpg':['.jpg','.jpeg']}
return [img for img in images if
_match_extension(img['name'],ext_map(type_))]
def _match_extension(filename,extension_list):
name,extension = os.path.splittext(filename.lower())
return extension in extension_list
def _save(images,format_):
if images:
if format_=='img':
_save_images(images)
else:
_save_json(images)
print('Done!')
else:
print('there are no images!')
def _save_images(images):
for img in images:
img_data = requests.get(img['url']).content
with open(img['name'],'wb') as f:
f.write(img_data)
def _save_json(images):
data = {}
for img in images:
img_data = requests.get(img['url']).content
b64_img_data = base64.b64encode(img_data)
str_img_data = b64_img_data.decode('utf-8')
data[img['name']]=str_img_data
with open('images.json','w') as ijson:
ijson.write(json.dump(data))
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='Scrape a webpage.')
parser.add_argument(
'-t',
'--type',
choices=['all', 'png', 'jpg'],
default='all',
help='The image type we want to scrape.')
parser.add_argument(
'-f',
'--format',
choices=['img', 'json'],
default='img',
help='The format images are saved to.')
parser.add_argument(
'url',
help='The URL we want to scrape for images.')
args = parser.parse_args()
args.url = str(input('enter the url\n'))
args.format = str(input('enter img or json \n'))
args.type = str(input('enter all or png or jpg'))
scrape(args.url,args.format,args.type)
i wants to run it in PyCharm and error i got is, 我想在PyCharm中运行它,但出现的错误是,
usage: scrape.py [-h] [-t {all,png,jpg}] [-f {img,json}] url
scrape.py: error: the following arguments are required: url
In order to pass parameters to your function in Pycharm, navigate to Run -> Edit Configurations
and set the parameters there. 为了将参数传递给您在Pycharm中的函数,请导航至
Run -> Edit Configurations
,然后在此处设置参数。
Alternatively, try invoking your program with parameters specified from the command line. 或者,尝试使用从命令行指定的参数来调用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.