【问题标题】:BeautifulSoup to access available bikes in DC bikeshareBeautifulSoup 可访问 DC 共享单车中的可用自行车
【发布时间】:2016-04-09 16:54:26
【问题描述】:

我是编程和 python 的新手,我正在尝试访问 DC 共享单车计划中给定站点的可用自行车数量。我相信最好的方法是使用 BeautifulSoup。好消息是数据可以在这里以一种干净的格式提供:https://www.capitalbikeshare.com/data/stations/bikeStations.xml

这是一个车站的例子:

<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>

我正在寻找 &lt;nbBikes&gt; 值。我有一个我认为是 python 脚本的开始,它将向我显示前 5 个站点的值(一旦我得到控制,我将解决选择我想要的站点)但它不返回任何值。这是脚本:

# 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

我相信我的问题(除了不理解如何在堆栈溢出问题中正确格式化代码)是nbikes = soup.select('#text') 的值不正确。但是,我似乎无法用任何东西代替“#text”来获得任何值,更不用说我想要的了。

我是否以正确的方式处理这个问题?如果是这样,我错过了什么?

谢谢

【问题讨论】:

  • 如果您正在获取 xml 格式的数据,您可以使用 xmletree 进行尝试。
  • 谢谢!看起来我的问题的很大一部分可能是尝试将 BeautifulSoup 用于 xml。我试图创建一个基本上是这样的新脚本:import xml.etree.ElementTree as ET tree = ET.ElementTree('https://www.capitalbikeshare.com/data/stations/bikeStations.xml') root = tree.getroot() print root
  • 这将返回 URL。尝试使用 root1 = ET.fromstring('station') print root1 之类的行更深入地了解结构会导致语法错误
  • 这也有一些很好的信息:plotsofdots.com/archives/68

标签: python python-2.7 web-scraping beautifulsoup


【解决方案1】:

此脚本创建一个结构为 [station_ID, bikes_remaining] 的字典。从这个开头修改: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]

【讨论】:

  • 除非对问题进行分类,否则不应作为答案
猜你喜欢
  • 1970-01-01
  • 2022-09-27
  • 1970-01-01
  • 1970-01-01
  • 2018-05-16
  • 2016-05-03
  • 2012-05-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多