2014-09-05 5 views
0

정보를 얻기 위해 json 문자열을 파싱하는 것이 처음입니다. json.loads를 사용하여 텍스트 블록을 분석했지만 타이틀을 얻는 방법을 찾는 데 어려움을 겪고 있습니다.Python에서 json 문자열 파싱

from alchemyapi import AlchemyAPI 
import json 

alchemyapi = AlchemyAPI() 

def run_alchemy_api(articleurl): 
    response = alchemyapi.entities('url',articleurl, { 'showSourceText':1, 'sourceText':'xpath', 'xpath':'//*[contains(@class,"title may-blank")][1]' }) 
    if response['status'] == 'OK': 
     print('## Response Object ##') 
     print(json.dumps(response, indent=4)) 
     json_string = json.dumps(response, indent=4) 
     titles = json.loads(json_string) 
     print('This is the decode test,') 
     print titles # <---- this is what I want to organize into a list 
    else: 
     print('Error in entity extraction call: ', response['statusInfo']) 

run_alchemy_api('http://www.reddit.com/r/worldnews/') 
난 그냥 u'text '카테고리의 구문을 분석하고 싶지만,이 출력의 일부 목록입니다

:

{u'status': u'OK', u'language': u'english', u'text': u'Lego is now the world\u2019s largest toymaker, as kids choose bricks over Barbie\n\nAfter convincing China to give up shark fin soup, Yao Ming sets out to save Africa\'s elephants from the ivory trade\n\nThree top ISIS lieutenants killed in US bombing raid\n\nAnonymous Really Wants a Cyberwar with the Islamic State\n\nBP found \'grossly negligent\' in 2010 Gulf oil spill\n\nA group of indigenous people in Brazil\'s Amazon region have detained and expelled loggers working illegally in their ancestral lands.\n\nAnti-ISIS flag-burning campaign launched by a trio of fearless Lebanese teens have ignited an Internet anti-terror sensation\n\nNova Scotia to ban fracking\n\nWHO and others criticised by numerous experts for misleading the public by publishing an ignorant and alarmist report into E-Cigarettes.\n\nRussia warns NATO not to offer membership to Ukraine\n\nKorean 20 year old dies in military service after a month of systematic beating, military is accused of covering up bullying\n\nNATO Chief to Russia: Pull Troops From Ukraine\n\nLarge asteroid to pass "very close" to Earth on Sunday\n\nNew dinosaur discovered! Ancient behemoth: Meet Dreadnoughtus, a supermassive dino\n\nThe U.N. nuclear watchdog said it has seen releases of steam and water indicating that North Korea may be operating a reactor, in the latest update on a plant that experts say could make plutonium for atomic bombs.\n\nWorld-first experiment achieves direct brain-to-brain communication in human subjects\n\nNATO allies to supply Ukraine with lethal military equipment\n\nUS doctor infected with Ebola heading to Nebraska\n\nNorth Korea\'s suicide rate among worst in world, says WHO report\n\nIslamic State Using Leaked Snowden Info To Evade Intelligence - U.S. Military Official Said Most Mid-Level And High-Ranking Islamic State Operators Have Virtually Disappeared, Giving No Hint As To Their Whereabouts Or Actions.\n\nEbola epidemic in West Africa is outpacing current responses.\u201cThe window of opportunity to stop Ebola from spreading widely throughout Africa and becoming a global threat for years to come is closing, but it is not yet closed,\u201d\n\nGrim Ebola Prediction: Outbreak Is Unstoppable for Now, MD Says\n\nFor the first time, scientists glimpse inside the cosmic nursery to see baby planets form\n\nCanadian beekeepers sue Bayer, Syngenta over neonicotinoid pesticides for over $400 million\n\nUkraine army on alert to repel possible rebel attack near Mariupol - military source', u'entities': [{u'relevance': u'0.803767', u'count': u'4', u'type': u'Country', u'text': u'Ukraine'}, {u'relevance': u'0.671762', u'count': u'3', u'type': u'Organization', u'disambiguated': {u'website': u'http://www.natoonline.org/', u'yago': u'http://yago-knowledge.org/resource/National_Association_of_Theatre_Owners', u'name': u'National Association of Theatre Owners', u'freebase': u'http://rdf.freebase.com/ns/m.031hx_', u'subType': [], u'dbpedia': u'http://dbpedia.org/resource/National_Association_of_Theatre_Owners'}, u'text': u'NATO'}, {u'relevance': u'0.564646', u'count': u'3', u'type': u'HealthCondition', u'text': u'Ebola'}, {u'relevance': u'0.543892', u'count': u'3', u'type': u'Region', u'text': u'West Africa'}, {u'relevance': u'0.521051', u'count': u'2', u'type': u'FieldTerminology', u'text': u'military equipment'}, {u'relevance': u'0.491148', u'count': u'2', u'type': u'Country', u'disambiguated': {u'website': u'http... and so on 

가 어떻게에 대한 그냥 가서 않습니다 여기

코드입니다 u'text '제목을 다음과 같이 추출합니다.

articles = [Lego is now the world\u2019s largest toymaker, as kids choose bricks over Barbie, After convincing China to give up shark fin soup, Yao Ming sets out to save Africa\'s elephants from the ivory trade ... etc.] 
+1

왜 응답을 덤프 한 다음 다시로드합니까? 응답을 직접 사용할 수 있습니다. – tdelaney

답변

1

텍스트의 제목이 두 줄로 나뉘어져 있습니다 (유닉스 스타일). 그래서 당신은 response dict에서 텍스트 키를 추출해야합니다 (json으로 변환하고 다시 파이썬으로 변환하지 마십시오) 그리고 그것을 제목으로 나눕니다.

text = response['text'] 
titles = text.split('\n\n') 
+0

너무 가까이! 야오밍 (Yao Ming)은 중국이 상어 지느러미 수프를 포기하도록 설득 한 후 상아 무역에서 아프리카 코끼리를 구하기 위해 나섰습니다. , ISIS 중위가 미국 폭격 공습에서 사망했습니다. ', U'Anonymous 정말 이슬람 국가와 사이버 전쟁을 원합니다.' –

+0

정확히 무엇을 원하니? 이? u'Lego는 지금 세계에서 가장 큰 통조림 제조업 자 "u는"아이들이 바비 인형보다 벽돌을 선택할 때 U "중국이 상어 지느러미 수프를 포기하도록 설득 한 후,"Yao Ming은 아프리카 코끼리를 아이보리 무역 "이라고 불렀다. ISIS 중위 대원이 미국 폭격 공습에서 사망했다. ', U'Anonymous 정말 이슬람 국가와 사이버 전쟁을 원한다.', ...] ' – semptic

+0

나는'U '를 제거하고 싶다. 그래서 아이들이 바비 인형보다 벽돌을 골랐기 때문에 레그는 세계에서 가장 큰 곡물 제조기가되었다. 중국이 상어 지느러미 수프를 포기하도록 설득 한 후, 야오밍은 아프리카의 코끼리를 구하기 위해 나섰다. 상아 무역에서 등 "] –

0

JSON을 구문 분석 한 후,이 같은 text를 추출 수동으로해야합니다 - ijson

+0

그냥 dict 및 표준 액세스는'json.loads (json_string) [ 'text']' – tdelaney

+0

입니다. 감사하지만이 형식과 같은 'u'Lego는 이제 세계에서 가장 큰 토이 메이커입니다. 야비 밍은 상어 지느러미 수프를 포기하도록 설득 한 후, 상아 무역에서 아프리카의 코끼리를 구하기 위해 밖으로 나섰다. \ n \ n ...] '[Title 1, 제목 2 등] –

0

응답은 당신이 큰 JSON 파일로 작업하는 경우

json.loads(json_string).get('text')

를 반복 JSON 파서를 사용하려고 python dict 및 'text'는 해당 키 중 하나입니다. 그냥 사용하십시오. 목록을 만드는 방법에는 여러 가지가 있습니다. 하나는 목록을 전달하고 성공하면 제목을 추가하는 것입니다.

def run_alchemy_api(articleurl, article_list): 
    response = alchemyapi.entities('url',articleurl, { 'showSourceText':1, 'sourceText':'xpath', 'xpath':'//*[contains(@class,"title may-blank")][1]' }) 
    if response['status'] == 'OK': 
     print(response['text']) 
     article_list.append(response['text']) 
    else: 
     print('Error in entity extraction call: ', response['statusInfo']) 


urls = [ 'url1', ...] 
titles = [] 
for url in urls: 
    run_alchmy_api(url, titles)