简体   繁体   English

我正在尝试使用 python 从 html 网站中提取一些数据

[英]im trying to extract some data out of html website using python

im trying to extract some data out of this page.我试图从这个页面中提取一些数据。 I got the data that i wanted but i wanna sepereate it.我得到了我想要的数据,但我想把它分开。 I got them like this: Adresse: Veteranenstr.我得到它们是这样的:地址:Veteranenstr。 21, 10119 Berlin - Mitte I wanna get the adresse and the House number for example Veteranenstr and 21 seperated. 21, 10119 Berlin - Mitte 我想得到地址和门牌号码,例如 Veteranenstr 和 21 分开。 The same goes for the light post seperated from the place.与该地方分开的灯柱也是如此。 does anybody got an idea how to get them seperated?有人知道如何将它们分开吗?

<div class="article-attributes">
        <h4 class="heading">Kinodetails</h4>
        <ul>
            <li>
                <span class="title">Adresse:</span>
                <span class="text">
                    <div class="first">
                        <span class="street-address">Veteranenstr. 21</span>
                    </div>
                    <div class="second">
                        <span class="postal-code">10119</span>&nbsp;
                        <span class="locality"> Berlin - Mitte </span>
                    </div>

and here is the code that i wrote for the extracting:这是我为提取编写的代码:

paga_soup = soup(page_htmll,"html.parser")
karak = paga_soup.findAll("div",{"class":"article-attributes"})
ka = karak[0]
dat = ka.findAll("li")

# suche nach Adresse
for by in dat:
    adresse = by.find_all('span')
    if (adresse[0].text == "Adresse:"):
        print('    ' + adresse[0].contents[0] + ' ' + adresse[2].text + ', '+ adresse[3].text + ' ' + adresse[4].text.strip())

You can try this, using a CSS selector to find the span that has the address, split on a space, and unpack them to be assigned to the variables.您可以试试这个,使用 CSS 选择器来查找具有地址的跨度,在空格上拆分,然后将它们解包以分配给变量。 It depends on how other addresses look like you might want to tweak the unpacking a bit.这取决于您可能想要稍微调整解包的其他地址的外观。

paga_soup = soup(page_htmll,"html.parser")
            karak = paga_soup.findAll("div",{"class":"article-attributes"})
            ka = karak[0]
            dat = ka.findAll("li")
            # suche nach Adresse
            for by in dat:
                address, number = by.find(".article-attributes .street-address").text.split()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM