2016-04-09 19 views
1

Programlama ve python konusunda yeniyim ve DC bikeshare programında belirli bir istasyondaki mevcut bisiklet sayısına erişmeye çalışıyorum. Bunu yapmanın en iyi yolunun BeautifulSoup ile olduğuna inanıyorum. https://www.capitalbikeshare.com/data/stations/bikeStations.xmlBeautifulSoup DC bikeshare'de mevcut bisikletlere erişmek için

Burada istasyonun bir örnek:: Ben <nbBikes> değeri arıyorum

<station> 
    <id>1</id> 
    <name>15th & S Eads St</name> 
    <terminalName>31000</terminalName> 
    <lastCommWithServer>1460217337648</lastCommWithServer> 
    <lat>38.858662</lat> 
    <long>-77.053199</long> 
    <installed>true</installed> 
    <locked>false</locked> 
    <installDate>0</installDate> 
    <removalDate/> 
    <temporary>false</temporary> 
    <public>true</public> 
    <nbBikes>7</nbBikes> 
    <nbEmptyDocks>8</nbEmptyDocks> 
    <latestUpdateTime>1460192501598</latestUpdateTime> 
</station> 

iyi haber verileri burada temiz bir biçim gibi görünen mevcut olmasıdır. İlk 5 istasyonun değerini gösterecek bir python betiğinin başlangıcı olacağını düşündüğüm şey vardı (bunu bir kez kontrol altına aldığım istasyonu seçmekle uğraşacağım) ama herhangi bir değer vermez.

# bikeShareParse.py - parses the capital bikeshare info page 


import bs4, requests 

url = "https://www.capitalbikeshare.com/data/stations/bikeStations.xml" 

res = requests.get(url) 
res.raise_for_status() 

#create the soup element from the file 
soup = bs4.BeautifulSoup("res.text", "lxml") 

# defines the part of the page we are looking for 
nbikes = soup.select('#text') 

#limits number of results for testing 
numOpen = 5 
for i in range(numOpen): 
     print nbikes 

benim sorunu (bir yığın taşması söz konusu kodu doğru biçimlendirmek için nasıl anlamak değil yanı sıra) nbikes = soup.select('#text') değeri yanlış olduğunu olduğuna inanıyoruz: İşte senaryo. Bununla birlikte, istediğim şeyleri bırakmadan, herhangi bir değer elde etmek için '#text' için herhangi bir şeyin yerini alamıyorum.

Buna doğru şekilde yaklaşıyor muyum? Öyleyse, neyi özlüyorum?

sayesinde

+0

Eğer xml formatında veri alıyorsanız eğer xmletree kullanarak bunu deneyin. – bhansa

+0

Teşekkürler! Benim sorunumun büyük bir kısmı xml için BeautifulSoup kullanmaya çalışıyor olabilir. Temelde bu yeni bir betik oluşturmaya çalıştım: 'import xml.etree.ElementTree ET tree = ET.ElementTree ('https://www.capitalbikeshare.com/data/stations/bikeStations.xml') kökü = tree.getroot() yazdırma kökü ' – mweinberg

+0

Bu URL'yi döndürür. Root1 = ET.fromstring ('station') print root1 'gibi satırlarla yapıya daha derinlemesine girmeye çalışıyorum sözdizimi hatalarını bana iletiyor – mweinberg

cevap

0

Bu komut yapısı [station_ID, bikes_remaining] bir sözlük oluşturur. Bu başından değiştirilir: http://www.plotsofdots.com/archives/68

# from http://www.plotsofdots.com/archives/68 


import xml.etree.ElementTree as ET 
import urllib2 

#we parse the data using urlib2 and xml 
site='https://www.capitalbikeshare.com/data/stations/bikeStations.xml' 
htm=urllib2.urlopen(site) 
doc = ET.parse(htm) 

#we get the root tag 
root=doc.getroot() 
root.tag 

#we define empty lists for the empty bikes 
sID=[] 
embikes=[] 
#we now use a for loop to extract the information we are interested in 
for country in root.findall('station'): 
    sID.append(country.find('id').text) 
    embikes.append(int(country.find('nbBikes').text)) 

#this just tests that the process above works, can be commented out 
#print embikes 
#print sID 

#use zip to create touples and then parse them into a dataframe 
prov=zip(sID,embikes) 

print prov[0] 
+1

Sorunu çözmedikçe cevap olmamalı – hd1