저는 파이썬에 완전히 익숙하지 않았으며 지금까지는 그 일을 해본 적이 없습니다. 나는이 프로그램에 붙어있다.이 프로그램은 키워드를 물어보고 사용 가능한 제목 목록에서 키워드를 검색하는 명령 줄 프로그램이라고 가정한다. json을 사용하여 API 정보를 사전에로드하고 검색 할 수있었습니다.파서 코드 만들기
내 주요한 문제는 내가 명령 행 프로그램으로 만들 수있는 argparser를 수행하는 방법을 모른다는 것이다.
도움 말?
import requests
import argparse
import json
from urllib.request import urlopen
def create_json_file_from_api(url):
request = urlopen(url)
data = request.read().decode("utf-8")
j_data = json.loads(data)
return j_data
json_data = create_json_file_from_api("http://hn.algolia.com/api/v1/search_by_date?tags=story&numericFilters=created_at_i>1488196800,created_at_i<1488715200")
print(json_data) #making sure the data pulled is correct
def _build_array_of_necessary_data(data, d=[]):
if 'hits' in data:
for t in data['hits']:
d.append({'title' : t.get('title'), 'points': t.get('points'), 'url' : t.get('url')})
_build_array_of_necessary_data(t,d)
return d
j = _build_array_of_necessary_data(json_data)
print(j) #testing the function above
def _search_titles_for_keywords(data, word, s=[]):
for c in data:
if word in c['title']:
s.append({'title' : c.get('title')})
return s
word = "the" #needs to be input by user
word.upper() == word.lower()
k = _search_titles_for_keywords(j, word)
print(k) #testing the function above
def _search_links_for_point_value(data, points, s=[]):
points = int(points)
for c in data:
if points <= c['points']:
s.append({'Title of article is' : c.get('title')})
return s
points = "7" #needs to be input by user
l = _search_links_for_point_value(j, points)
print(l)
고맙습니다.하지만 포인트 검색과 키워드가 일치하는 제목 만있는 목록을 얻으려면 어떻게해야합니까? a, b의 경우 zip (k, l) : print (a, b) 코드 끝 부분에? – KRose