Python webscrape导出为csv - 没有数据

时间:2017-12-11 19:43:41

标签: python web-scraping

我有一个从网站上提取的脚本,它似乎不想将数据写入csv。 我假设我的for循环有问题,以及它是如何写出来的。有人会看看我,看看你会如何改变它?我在这里错过了什么。

感谢您的帮助。

下面是python脚本 - 再次,我认为问题在于底部的for循环。

from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup

# setting my_url to the wesite
urls = ['https://www.extraspace.com/Storage/Facilities/US/North_Carolina/Charlotte/1000000398/Facility.aspx'
    , 'https://www.extraspace.com/Storage/Facilities/US/North_Carolina/Charlotte/1000000404/Facility.aspx']

filename = "extraspace.csv"
open(filename, 'w').close()
f = open(filename, "a")
num = 0

headers = "unit_size, size_dim1, unit_type, online_price, reg_price, street_address, store_city, store_postalcode\n"

f.write(headers)

for my_url in urls:
    # Opening up connection, grabbing the page
    uClient = uReq(my_url)

    # naming uClient to page_html
    page_html = uClient.read()

    # closing uClient
    uClient.close()

    # this does my html parsing
    page_soup = soup(page_html, "html.parser")

    # setting container to capture where the actual info is using inspect element

    #grabs each product
    containers = page_soup.findAll("div", {"itemprop": "makesOffer"})

    #grabs address
    store_locator = page_soup.findAll("div", {"itemprop": "address"})

    f.write("website " + str(num) + ": \n")
    for container in containers:
        for store_location in store_locator:
            street_address = store_location.findAll("span", {"itemprop": "streetAddress"})
            store_city = store_location.findAll("span", {"itemprop": "addressLocality"})
            store_postalcode = store_location.findAll("spand", {"itemprop": "postalCode"})
            title_container = container.findAll("div", {"class": "size RamaGothicSemiBold"})
            size_dim = container.findAll("div", {"itemprop": "description"})
            unit_container = container.ul.li
            unit_type = container.text
            online_price = container.findAll("div", {"itemprop": "price"})
            reg_price = container.findAll("div", {"class": "rate strikeout"})

        for item in zip(title_container, size_dim, unit_type, online_price, reg_price, street_address, store_city, store_postalcode):
            csv = item[0].text + "," + item[1].text + "," + item[2] + "," + item[3].text + "," + item[4].text + "," + item[5].text + "," + item[6].text + "," + item[7].text + "\n"
            f.write(csv)
    num += 1

这是容器的HTML:

<div itemprop="makesOffer" itemscope="" itemtype="http://schema.org/Offer">
    <div itemprop="itemOffered" itemscope="" itemtype="http://schema.org/Product">
        <div class="guide">
            <div class="size-help-lnk size-guide hidden" data-locker="False" data-square-feet="25">Size Help</div>
            <div alt="5x5" class="video-btn-5x5 video-link" onclick="trackSC('UnitListingVideo');"></div>
        </div>
        <div class="size RamaGothicSemiBold">
            <div itemprop="description">5' x 5'</div>
            <div>SMALL</div>
        </div>
        <div class="features">
            <ul itemprop="description">
                <li><i class="check-icon"></i>Enclosed Storage</li>
                <li><i class="check-icon"></i>Indoor</li>
                <li><i class="check-icon"></i>1st Floor Access</li>
            </ul>
        </div>
    </div>
    <div>
        <div class="rate strikeout">
            <div><span style="width:100%;"></span>$57</div><span class="StreetRate">IN-STORE</span></div>
        <div class="rate">
            <div content="35.00" itemprop="price">$35
                <meta content="USD" itemprop="priceCurrency" />
            </div><span class="WebRate">WEB RATE</span></div>
        <div class="promo"><span style="color:#000;">Act fast:<br/>Limited units</span></div>
    </div>
    <a class="btn btn-orange cta-test is-vehicle" href="https://www.extraspace.com/Storage/ReserveOrHold.aspx?uid=a0GC000000tUNupMAG" id="ctl00_mContent_UnitListPopular_ctrl0_hlReserveLink" onclick="upDown('unitRows|8506|1;05X05|NDN|57|35| | ; | | | | | ; | | | | | ;05X05|CDN|71|48| | ;05X07|CDN|74|50| | ');">RESERVE</a>
    <div class="clear"></div>
    <link href="http://schema.org/OnlineOnly" itemprop="availability">
    </link>
</div>

以下是store_locator的html;

[ < div itemprop = "address"
    itemscope = ""
    itemtype = "http://schema.org/PostalAddress" >
    <
    span id = "ctl00_mContent_lbAddress"
    itemprop = "streetAddress" > 3304 Eastway Dr < br / > Ste D < /span><br/ >
    <
    span id = "ctl00_mContent_lbCity"
    itemprop = "addressLocality" > Charlotte < /span>, <
    span id = "ctl00_mContent_lbState"
    itemprop = "addressRegion" > NC < /span> <
    span id = "ctl00_mContent_lbPostalCode"
    itemprop = "postalCode" > 28205 < /span> <
    /div>]

0 个答案:

没有答案