python webscrape清洁

时间:2017-11-06 14:12:37

标签: python beautifulsoup code-formatting

我正在使用python和beautifulsoup来捕获并打印出以下内容: 小 5' x 10' 室外单元/驾车通道 $ 56 /月。 $ 70店内

我设法让它正确打印出单位尺寸(小)和单位类型(外部单位/驱动器访问),然而,其他人正在打印出正确的数据以及" div类文本"

任何人都知道我怎样才能正确抓住它? 我将添加以下代码;

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


#setting my_url to the wesite
my_url = 'https://www.publicstorage.com/north-carolina/self-storage-
charlotte-nc/28206-self-storage/2334?
lat=35.23552&lng=-80.83296&clp=1&sp=Charlotte|35.2270869|-80.8431267&ismi=1'

#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("li",{"class":"srp_res_row plp"})

filename = "product.csv"
f = open(filename, "w")

headers = "unit_size, size_dim, unit_type, online_price, reg_price\n"

f.write(headers)

for container in containers:
    title_container = container.div.div
    unit_size = title_container.text
    size_dim = container.findAll("div", {"class":"srp_label srp_font_14"})
    unit_container = container.li
    unit_type = unit_container.text
    online_price = container.findAll("div", {"class":"srp_label alt-price"})
    reg_price = container.findAll("div", {"class":"reg-price"})


    print("unit_size: " + str(unit_size))
    print("size_dim: " + str(size_dim))
    print("unit_type: " + str(unit_type))
    print("online_price: " + str(online_price))
    print("reg_price: " + str(reg_price))

    f.write(str(unit_size) + "," +str(size_dim) + "," +str(unit_type) + "," 
    +str(online_price) + "," +str(reg_price) + "\n")
f.close()



<li class="srp_res_row plp">
    <div class="srp_res_clm srp_clm160">
        <div class="srp_label plp">Small</div>
        <div class="srp_v-space_3"></div>
        <div class="srp_label srp_font_14" style="padding-left: 5px;">5' x 10'</div>
        <div class="srp_v-space_3"></div>
    </div>
    <div class="srp_res_clm srp_clm120">
        <ul class="srp_list">
            <li>Outside unit/Drive-up access</li>
        </ul>
    </div>
    <div class="srp_res_clm srp_clm90">
        <div class="srp_label">$1<span class="srp_label_symbol">†</span></div>
        <div class="srp_v-space_10">1st Month</div>
    </div>
    <div class="srp_res_clm srp_clm90">
        <div class="srp_label alt-price">$56/mo.</div>
        <div class="online-special">Online Special<span class="srp_label_symbol">†</span></div>
        <div class="srp_v-space_15"></div>
        <div class="reg-price">$70 In-store</div>
    </div>
    <div class="srp_res_clm srp_clm100 srp_vcenter"><a class="srp_continue unit-no-deposit" data-deposit-amount="0" data-deposit-days="0" data-features="Outside unit/Drive-up access" data-marketing-size="5x10" data-ppk="altproduct_price" data-promotionid="132" data-siteid="2334" data-size-description="5' x 10'" data-sizeid="613573" data-wc2-unit="false" href="/ReservationDetails.aspx?st=2334&amp;sz=613573&amp;key=[rnd]&amp;location=&amp;plp=1&amp;rk=&amp;ismi=1&amp;sp=Charlotte%7c35.2270869%7c-80.8431267&amp;clp=1"><img alt="Continue" src="/images/srp-cont-new-80.png" style="width: 80px; height: 32px"/></a></div>
</li>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

find_all返回一个ResultSet对象,您可以使用for循环迭代它。

试试这段代码:

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


#setting my_url to the wesite
my_url = 'https://www.publicstorage.com/north-carolina/self-storage-charlotte-nc/28206-self-storage/2334?lat=35.23552&lng=-80.83296&clp=1&sp=Charlotte|35.2270869|-80.8431267&ismi=1'

#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("li",{"class":"srp_res_row plp"})

filename = "product.csv"
f = open(filename, "w")

headers = "unit_size, size_dim, unit_type, online_price, reg_price\n"

f.write(headers)

for container in containers:
    title_container = container.div.div
    unit_size = title_container.text
    size_dim = container.findAll("div", {"class":"srp_label srp_font_14"})
    unit_container = container.li
    unit_type = unit_container.text
    online_price = container.findAll("div", {"class":"srp_label alt-price"})
    reg_price = container.findAll("div", {"class":"reg-price"})



    print("unit_size: " + str(unit_size))
    print("size_dim: {}".format("".join(list(i.text for i in size_dim))))  #edited line
    print("unit_type: " + str(unit_type))
    print("Online Price: {}".format("".join(list(i.text for i in online_price)))) #edited line
    print("Online Price: {}".format("".join(list(i.text for i in reg_price)))) #edited line


    f.write(str(unit_size) + "," +str(size_dim) + "," +str(unit_type) + ","
    +str(online_price) + "," +str(reg_price) + "\n")
f.close()

输出:

unit_size: Small
size_dim: 5' x 10'
unit_type: Outside unit/Drive-up access
Online Price: $52/mo.
Online Price: $64 In-store
unit_size: Medium
size_dim: 5' x 15'
unit_type: Outside unit/Drive-up access
Online Price: $80/mo.
Online Price: $100 In-store
  

根据评论更新了代码:

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


#setting my_url to the wesite
my_url = 'https://www.publicstorage.com/north-carolina/self-storage-charlotte-nc/28206-self-storage/2334?lat=35.23552&lng=-80.83296&clp=1&sp=Charlotte|35.2270869|-80.8431267&ismi=1'

#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("li",{"class":"srp_res_row plp"})

filename = "product.csv"
f = open(filename, "w")

headers = "unit_size, size_dim1, unit_type, online_price, reg_price\n"

f.write(headers)

for container in containers:
    title_container = container.div.div
    unit_size = title_container.text
    size_dim = container.findAll("div", {"class":"srp_label srp_font_14"})
    unit_container = container.li
    unit_type = unit_container.text
    online_price = container.findAll("div", {"class":"srp_label alt-price"})
    reg_price = container.findAll("div", {"class":"reg-price"})


    for item in zip(unit_size,size_dim,unit_container,online_price,reg_price):
        csv=item[0] + "," + item[1].text + "," + item[2] + "," + item[3].text + "," + item[4].text + "\n"
        f.write(csv)