我有这种结构。
<div id="one" class="tab-pane active">
<div class="item-content">
<a href="/mobilni-uredjaji.4403.html">
<div class="item-primary">
<div class="sticker-small">
<span class=""></span>
</div>
<div class="sticker-small-lte">
<span class="lte"></span>
</div>
<div class="item-photo">
<img src="/upload/images/thumbs/devices/SAMG935F/SAMG935F_image001_220x230.png" alt="Samsung Galaxy S7 edge">
</div>
<div class="item-labels">
<span class="item-manufacturer">Samsung</span>
<span class="item-title">Galaxy S7 edge</span>
</div>
</div>
<div class="item-secondary">
<span class="item-price">94.000<span class="currency">currency!</span></span>
<span class="item-package"> uz! Sigurica 1500</span>
<span class="item-installments">na! 24 rate_po!</span>
<span class="value">100 currency!</span>
<span class="item-available">device_is_available_on_webshop_list!</span>
</div>
</a>
</div>
<div class="item-content">
<a href="/mobilni-uredjaji.4403.html">
<div class="item-primary">
<div class="sticker-small">
<span class=""></span>
</div>
<div class="sticker-small-lte">
<span class="lte"></span>
</div>
<div class="item-photo">
<img src="/upload/images/thumbs/devices/SAMG935F/SAMG935F_image001_220x230.png" alt="Samsung Galaxy S7 edge">
</div>
<div class="item-labels">
<span class="item-manufacturer">Samsung</span>
<span class="item-title">Galaxy S7 edge</span>
</div>
</div>
<div class="item-secondary">
<span class="item-price">94.000<span class="currency">currency!</span></span>
<span class="item-package"> uz! Sigurica 1500</span>
<span class="item-installments">na! 24 rate_po!</span>
<span class="value">100 currency!</span>
<span class="item-available">device_is_available_on_webshop_list!</span>
</div>
</a>
</div>
-----same structure ----
</div>
我的目标是带有class =“item-labels”的div以及相应的数据:
和div为“item-secondary”类,span标记下的数据为class =“item-price”,94.000。
我需要输出完全正确:
到目前为止,使用此代码我得到的前两个数据没有价格。我有点被困在这里因为我没有做过很长一段时间。 请提示一下? 代码是:
from bs4 import BeautifulSoup
import re
import pymysql
import MySQLdb
import urllib
#rl = "http://www.vipmobile.rs/mobilni-uredjaji.2631.html#tarifgroup-1|devicetype-1|minprice-0|maxprice-124800|brand-0|model-0"
url = "file:///C:/Users/zika/Desktop/one.html"
html = urllib.urlopen(url)
page = html.read()
#print(page)
# db = MySQLdb.connect(host = 'localhost',
# user = 'root',
# passwd = '123456',
# db = 'lyrics')
soup = BeautifulSoup(page, 'html.parser')
#mobData = soup.find("div", {"class": "bxslider items"}).find_all("div", {"class": "item-content"})
#for mobMan in soup.find("div", {"class": "tab-pane active"}).findAll("span")
labelData = soup.find("div", {"class": "tab-pane active"}).find_all("div", {"class": "item-content"})
labelPrice = soup.find("div", {"class": "tab-pane active"}).find_all("span", class_="item-price")
for label in labelData:
print(label.contents[1].find("div", {"class": "item-labels"}).getText())
for price in labelPrice:
print(price.getText())
input("\n\nPress the enter key to exit!")
答案 0 :(得分:1)
你可以尝试:
from bs4 import BeautifulSoup
soup = BeautifulSoup(source, "html.parser")
div1 = soup.find("div", { "class" : "item-labels" }).findAll('span', { "class" : "item-manufacturer" })
div2 = soup.find("div", { "class" : "item-labels" }).findAll('span', { "class" : "item-title" })
div3 = soup.find("div", { "class" : "item-secondary" }).findAll('span', { "class" : "item-price" })
for i,j,k in zip(div1,div2,div3):
print i.text
print j.text
print k.text.replace("currency!",'')
注意:在上面的代码中,source
使用了您在帖子中提供的结构。
这将提供以下输出:
Samsung
Galaxy S7 edge
94.000