删除标签-美丽的汤

travisrpetersen

我遇到了一个问题,我的代码从带有标签的XML返回我想要的信息,而我只希望标签之间的信息。我的输出看起来像

[<weekendingdate>2015-05-02</weekendingdate>]

但应该是2015-05-02。谢谢您的帮助!

以下是我的尝试和XML代码。

试图:

from urllib2 import urlopen
import requests
from bs4 import BeautifulSoup
from bs4 import BeautifulStoneSoup
import csv
import pandas 
from lxml import etree

r = requests.get(url)
soup = BeautifulSoup(r.content)

date  = []
average_listing_price = []
median_listing_price = []
number_of_properties = []
weekend = soup.findAll('listingstat')

for z in weekend:
    date.append(z.findAll('weekendingdate')[0].text)
    average_listing_price.append(z.findAll('averagelistingprice')[0].text)
    median_listing_price.append(z.findAll('medianlistingprice')[0])
    number_of_properties.append(z.findAll('numberofproperties')[0])

# Zip em up
master = zip(date, average_listing_price, median_listing_price, number_of_properties)

with open('trulia.csv','a') as f:
    writer = csv.writer(f, lineterminator='\n')
    writer.writerows(master)

XML:

<response>
<TruliaStats>
<location>
<zipCode>10019</zipCode>
<state>NY</state>
<searchResultsURL>http://www.trulia.com/NY/New_York/10019/</searchResultsURL>
<heatMapURL>
http://www.trulia.com/home_prices/New_York/New_York_County-heat_map/
</heatMapURL>
</location>
<trafficStats>...</trafficStats>
<listingStats>
<listingStat>
<weekEndingDate>2015-05-02</weekEndingDate>
<listingPrice>
<subcategory>
<type>All Properties</type>
<numberOfProperties>148</numberOfProperties>
<medianListingPrice>1964571</medianListingPrice>
<averageListingPrice>4352399</averageListingPrice>
</subcategory>
<subcategory>
<type>1 Bedroom Properties</type>
<numberOfProperties>56</numberOfProperties>
<medianListingPrice>1177857</medianListingPrice>
<averageListingPrice>1870867</averageListingPrice>
</subcategory>
<subcategory>
<type>2 Bedroom Properties</type>
<numberOfProperties>47</numberOfProperties>
<medianListingPrice>2541571</medianListingPrice>
<averageListingPrice>3815937</averageListingPrice>
</subcategory>
<subcategory>
<type>3 Bedroom Properties</type>
<numberOfProperties>20</numberOfProperties>
<medianListingPrice>5365000</medianListingPrice>
<averageListingPrice>8921877</averageListingPrice>
</subcategory>
<subcategory>
<type>4 Bedroom Properties</type>
<numberOfProperties>10</numberOfProperties>
<medianListingPrice>9700000</medianListingPrice>
<averageListingPrice>13165615</averageListingPrice>
</subcategory>
<subcategory>
<type>5 Bedroom Properties</type>
<numberOfProperties>2</numberOfProperties>
<medianListingPrice>17821429</medianListingPrice>
<averageListingPrice>17821429</averageListingPrice>
</subcategory>
<subcategory>
<type>8 Bedroom Properties</type>
<numberOfProperties>1</numberOfProperties>
<medianListingPrice>5400000</medianListingPrice>
<averageListingPrice>5400000</averageListingPrice>
</subcategory>
</listingPrice>
</listingStat>
<listingStat>
<weekEndingDate>2015-05-09</weekEndingDate>
<listingPrice>
<subcategory>
<type>All Properties</type>
<numberOfProperties>140</numberOfProperties>
<medianListingPrice>1998429</medianListingPrice>
<averageListingPrice>4582774</averageListingPrice>
</subcategory>
<subcategory>
<type>1 Bedroom Properties</type>
<numberOfProperties>49</numberOfProperties>
<medianListingPrice>1270143</medianListingPrice>
<averageListingPrice>1701269</averageListingPrice>
</subcategory>
<subcategory>
<type>2 Bedroom Properties</type>
<numberOfProperties>41</numberOfProperties>
<medianListingPrice>2366786</medianListingPrice>
<averageListingPrice>3666856</averageListingPrice>
</subcategory>
<subcategory>
<type>3 Bedroom Properties</type>
<numberOfProperties>22</numberOfProperties>
<medianListingPrice>5200714</medianListingPrice>
<averageListingPrice>8204036</averageListingPrice>
</subcategory>
<subcategory>
<type>4 Bedroom Properties</type>
<numberOfProperties>10</numberOfProperties>
<medianListingPrice>12735714</medianListingPrice>
<averageListingPrice>15974589</averageListingPrice>
</subcategory>
<subcategory>
<type>5 Bedroom Properties</type>
<numberOfProperties>2</numberOfProperties>
<medianListingPrice>22000000</medianListingPrice>
<averageListingPrice>22000000</averageListingPrice>
</subcategory>
<subcategory>
<type>8 Bedroom Properties</type>
<numberOfProperties>1</numberOfProperties>
<medianListingPrice>5400000</medianListingPrice>
<averageListingPrice>5400000</averageListingPrice>
</subcategory>
</listingPrice>
</listingStat>
<listingStat>
<weekEndingDate>2015-05-16</weekEndingDate>
<listingPrice>
<subcategory>
<type>All Properties</type>
<numberOfProperties>96</numberOfProperties>
<medianListingPrice>1921500</medianListingPrice>
<averageListingPrice>5081798</averageListingPrice>
</subcategory>
<subcategory>
<type>1 Bedroom Properties</type>
<numberOfProperties>36</numberOfProperties>
<medianListingPrice>1370143</medianListingPrice>
<averageListingPrice>1640854</averageListingPrice>
</subcategory>
<subcategory>
<type>2 Bedroom Properties</type>
<numberOfProperties>27</numberOfProperties>
<medianListingPrice>2321786</medianListingPrice>
<averageListingPrice>4798047</averageListingPrice>
</subcategory>
<subcategory>
<type>3 Bedroom Properties</type>
<numberOfProperties>13</numberOfProperties>
<medianListingPrice>5226786</medianListingPrice>
<averageListingPrice>9402061</averageListingPrice>
</subcategory>
<subcategory>
<type>4 Bedroom Properties</type>
<numberOfProperties>9</numberOfProperties>
<medianListingPrice>13003571</medianListingPrice>
<averageListingPrice>15415506</averageListingPrice>
</subcategory>
<subcategory>
<type>5 Bedroom Properties</type>
<numberOfProperties>1</numberOfProperties>
<medianListingPrice>30357143</medianListingPrice>
<averageListingPrice>30357143</averageListingPrice>
</subcategory>
</listingPrice>
</listingStat>
<listingStat>
<weekEndingDate>2015-05-23</weekEndingDate>
<listingPrice>
<subcategory>
<type>All Properties</type>
<numberOfProperties>48</numberOfProperties>
<medianListingPrice>2569286</medianListingPrice>
<averageListingPrice>5416430</averageListingPrice>
</subcategory>
<subcategory>
<type>1 Bedroom Properties</type>
<numberOfProperties>14</numberOfProperties>
<medianListingPrice>1930357</medianListingPrice>
<averageListingPrice>2202226</averageListingPrice>
</subcategory>
<subcategory>
<type>2 Bedroom Properties</type>
<numberOfProperties>19</numberOfProperties>
<medianListingPrice>2955500</medianListingPrice>
<averageListingPrice>5074193</averageListingPrice>
</subcategory>
<subcategory>
<type>3 Bedroom Properties</type>
<numberOfProperties>6</numberOfProperties>
<medianListingPrice>4891071</medianListingPrice>
<averageListingPrice>7457568</averageListingPrice>
</subcategory>
<subcategory>
<type>4 Bedroom Properties</type>
<numberOfProperties>4</numberOfProperties>
<medianListingPrice>15560714</medianListingPrice>
<averageListingPrice>14827381</averageListingPrice>
</subcategory>
<subcategory>
<type>5 Bedroom Properties</type>
<numberOfProperties>1</numberOfProperties>
<medianListingPrice>31750000</medianListingPrice>
<averageListingPrice>31750000</averageListingPrice>
</subcategory>
</listingPrice>
</listingStat>
<listingStat>
<weekEndingDate>2015-05-30</weekEndingDate>
<listingPrice>
<subcategory>
<type>All Properties</type>
<numberOfProperties>18</numberOfProperties>
<medianListingPrice>3550000</medianListingPrice>
<averageListingPrice>4146640</averageListingPrice>
</subcategory>
<subcategory>
<type>1 Bedroom Properties</type>
<numberOfProperties>6</numberOfProperties>
<medianListingPrice>2523333</medianListingPrice>
<averageListingPrice>2930370</averageListingPrice>
</subcategory>
<subcategory>
<type>2 Bedroom Properties</type>
<numberOfProperties>5</numberOfProperties>
<medianListingPrice>4415000</medianListingPrice>
<averageListingPrice>4215333</averageListingPrice>
</subcategory>
<subcategory>
<type>3 Bedroom Properties</type>
<numberOfProperties>3</numberOfProperties>
<medianListingPrice>5079167</medianListingPrice>
<averageListingPrice>3984722</averageListingPrice>
</subcategory>
<subcategory>
<type>4 Bedroom Properties</type>
<numberOfProperties>2</numberOfProperties>
<medianListingPrice>9791667</medianListingPrice>
<averageListingPrice>10243750</averageListingPrice>
维卡斯·欧哈(Vikas Ojha)

您的代码运行正常。必须是您要尝试打印作为列表的变量date,并且它的str形式会添加这些[]。如果要打印第一个元素,请使用

print date[0]

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Python-美丽的汤-删除标签

来自分类Dev

美丽汤元内容标签

来自分类Dev

美丽的汤遍历html标签

来自分类Dev

标签中的美丽汤标

来自分类Dev

从美丽的汤中获取标签“ a”

来自分类Dev

标签中的美丽汤标

来自分类Dev

美丽的汤-获取标签内容

来自分类Dev

标签内的美丽汤文本

来自分类Dev

Python,美丽的汤,<br> 标签

来自分类Dev

美丽的汤返回标签和文本

来自分类Dev

美丽的汤,通过带有标签的标签内容进行搜索

来自分类Dev

美丽的汤返回关闭标签而不是标签文本

来自分类Dev

美丽的汤find_all()返回奇数标签而不是结果

来自分类Dev

美丽的汤只是获得标签内的价值

来自分类Dev

在带有美丽汤的特定标签后获取文本

来自分类Dev

使用正则表达式查找标签的美丽汤?

来自分类Dev

美丽的汤标签有冒号。需要提取温度值。

来自分类Dev

如何使用美丽的汤4从span标签提取文本?

来自分类Dev

Findall 到 div 标签使用美丽的汤产生空白返回

来自分类Dev

无法使用美丽的汤获得标签的价值

来自分类Dev

通过文本而不是标签在美丽的汤中查找项目

来自分类Dev

使用美丽的汤在 div 标签下进行网页抓取

来自分类Dev

美丽的汤不能用括号删除电话号码

来自分类Dev

如何在我的美丽汤中删除分隔符“”

来自分类Dev

如何删除所有“ document.write('');” 和美丽的汤

来自分类Dev

美丽的汤,不包括父 <li> 标签上的内部 <li> 和 <ul> 标签 .getText()

来自分类Dev

查找与美丽汤一起使用的第三个出现的<p>标签

来自分类Dev

美丽的汤-`findAll`不能捕获SVG中的所有标签(`ElementTree`可以)

来自分类Dev

在美丽的汤中查找下一个div标签

Related 相关文章

热门标签

归档