删除标签 - 美丽的汤

时间:2015-06-11 12:37:39

标签: xml python-2.7 beautifulsoup

我遇到了一个问题,即我的代码使用标签返回我想要的信息,而我只需要标签之间的信息。我的输出看起来像

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

1 个答案:

答案 0 :(得分:0)

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

print date[0]