Python에 대한 초보자 용서를 용서해주십시오. BeautifulSoup을 사용하여 XML 파일의 특정 요소를 반복해야합니다.Python과 BeautifulSoup를 사용하여 XML 파일에서 사전 만들기
날씨 웹 사이트에서 만든 XML 파일에서 정보를 얻으려고하고 있습니다. 지금 당장 XML을 저장하고 있습니다.
def aber_forcast():
url = "http://api.met.no/weatherapi/locationforecast/1.9/?lat=52.41616;lon=-4.064598"
response = requests.get(url)
xml_text=response.text
soup= bs4.BeautifulSoup(xml_text, "xml")
f = open('file.xml', "w")
f.write(soup.prettify())
f.close()
return (soup)
'symbol id'요소에서 발생 횟수를 계산하려고합니다. 심볼 아이디의 그래프를 생성해야하며 XML 전체에서 얼마나 많은 시간이 걸릴지를 결정해야합니다. 나는 모든 symbol_id를 하나의리스트로 가져올 수있다;
with open ('file.xml') as file:
soup = bs4.BeautifulSoup(file, "xml")
symbol_id = soup.find_all("symbol")
print(symbol_id)
'클라우드', '비'등 및 유래를 통해보고하는 데, 그것과 함께 간다 관련된 ID 번호, 나는 내가 사전을 만들어야합니다 아래의 코드와 같은 뭔가가 있으리라 믿고있어하는 연관된 숫자와 ID는 반복 횟수를 계산합니다.
def parseLog(file):
file = sys.argv[1]
handler = open(file).read()
soup = Soup(handler)
for sym in soup.findAll('symbol'):
msg_attrs = dict(sym.attrs)
f_user = sym.find('symbol id').user
f_user_dict = dict(f_user.attrs)
print ((f_user_dict[u'symbols'], sym.find('number').decodeContents())
질문이별로 의미가 없으면 모든 도움이나 조언은 대단히 죄송합니다.
하나 개의 라인에서이 작업을 수행 할 수 있습니다. 고마워. :) –
문제 없습니다. 내가 추가 한 한 줄의 대답을보십시오. –