从webservice获取xml?

2024-09-28 23:27:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从this site获取数据

然后用一些。很抱歉没有复制粘贴它,但它是一个很长的xml。到目前为止,我试图通过以下方式获取这些数据:

from urllib.request import urlopen
url = "http://degra.wi.pb.edu.pl/rozklady/webservices.php?"
s = urlopen(url)
content = s.read()

由于print(content)看起来不错,现在我想从中获取一个数据

^{pr2}$

如何处理这些数据以便于使用?在


Tags: 数据fromimporthttpurlrequest方式site
1条回答
网友
1楼 · 发布于 2024-09-28 23:27:58

你可以用漂亮的汤和捕捉你想要的标签。下面的代码应该可以让你开始!在

import pandas as pd
import requests
from bs4 import BeautifulSoup

url = "http://degra.wi.pb.edu.pl/rozklady/webservices.php?"

# secure url content
response = requests.get(url).content
soup = BeautifulSoup(response)

# find each tabela_rozklad
tables = soup.find_all('tabela_rozklad')

# for each tabela_rozklad looks like there is 12 nested corresponding   tags
tags = ['dzien', 'godz', 'ilosc', 'tyg', 'id_naucz', 'id_sala',
    'id_prz', 'rodz', 'grupa', 'id_st', 'sem', 'id_spec']

# initialize empty dataframe
df = pd.DataFrame()

# iterate over each tabela_rozklad and extract each tag and append to pandas dataframe
for table in tables:
    all = map(lambda x: table.find(x).text, tags)
    df = df.append([all])

# insert tags as columns
df.columns = tags

# display first 5 rows of table
df.head()

# and the shape of the data
df.shape # 665 rows, 12 columns

# and now you can get to the information using traditional pandas  functionality

# for instance, count observations by rodz
df.groupby('rodz').count()

# or subset only observations where rodz = J
J = df[df.rodz == 'J']

相关问题 更多 >