Ich habe den folgenden Code und es funktioniert ganz gut, dank der Hilfe von jedermann hier. Ich habe versucht, nach einem relevanten Thread zu suchen, der die Frage beantwortet, die ich habe, aber ich konnte einen nicht finden, also hier geht es.Webscraping Mehrere Seiten - Python
Wie kann ich mehrere Seiten zu diesem Code hinzufügen, damit er in eine CSV-Datei entsprechend gedruckt wird?
Hier sind ein paar der Seiten, die ich hinzufügen möchte (es wäre mehr als nur die extra 3) Vielen Dank für Ihre Hilfe.
Unten ist der Code:
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"})
store_locator = page_soup.findAll("div", {"itemprop":"address"})
filename = "product.csv"
f = open(filename, "w")
headers = "unit_size, size_dim1, unit_type, online_price, reg_price,
street_address, store_city\n"
f.write(headers)
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"})
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,street_address,stor
e_city):
csv=item[0] + "," + item[1].text + "," + item[2] + "," +
item[3].text + "," + item[4].text + "," + item[5].text + "," + item[6].text
+ "\n"
f.write(csv)
Hier ist die HTML-Skript;
<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&sz=613573&key=[rnd]&location=&plp=1&rk=&ismi=1&sp=Charlotte%7c35.2270869%7c-80.8431267&clp=1"><img alt="Continue" src="/images/srp-cont-new-80.png" style="width: 80px; height: 32px"/></a></div>
</li>
Sie die URLs in einer Liste und Schleife über jede speichern URL und dann verschrotten und speichern Sie die CSV. – Ali
@Ali - danke für die schnelle Antwort. Würde es Ihnen etwas ausmachen, mir zu zeigen, wie ich das mache? –
Bitte beachten Sie die Antwort unten. – Ali