【问题标题】:How can I get the first string from a div that has a div embedded beautifulsoup4如何从嵌入了 div 的 beautifulsoup4 的 div 中获取第一个字符串
【发布时间】:2019-12-01 07:09:29
【问题描述】:

我正在尝试从网站中提取价格。

我编写的代码可以做到这一点,但是当网站的价格也显示旧价格时,它会返回“none”而不是价格字符串。

这是一个没有旧价格的代码示例(我的代码以字符串形式返回)

<div class="xl-price rangePrice">
                            535.000 €  
                        </div>

这是带有旧价格的代码示例(我的代码返回为“无”)

    < div


class ="xl-price rangePrice" >


487.000 €
< span


class ="old-price" > 497.000 € < br > < / span >

< / div >

我试图从中提取代码的页面:pagelink

我的代码:

prices = []
for items in soup.find_all("div", {"class": "xl-price rangePrice"}):
    prices.append(items.string)

print(prices)

我遇到的另一个问题是它返回如下值:

'\r\n\t\t\t\t\t\t\t\t298.000 € \r\n\t\t\t\t\t\t\t', '\r\n\t\t\t\t\t\t\t\t145.000 € \r\n\t\t\t\t\t\t\t'

当我只想要数字时。

不胜感激!

【问题讨论】:

  • .string.text 不同。你可以阅读更多关于前者的here,你可能想要的是后者。

标签: python beautifulsoup text-extraction data-extraction


【解决方案1】:
import requests
from bs4 import BeautifulSoup

r = requests.get(
    'https://www.immoweb.be/en/search/apartment/for-sale/leuven/3000')
soup = BeautifulSoup(r.text, 'html.parser')

for item in soup.findAll('div', attrs={'class': 'xl-price rangePrice'}):
    item = item.contents[0]
    print(item.strip()[0:-1])

输出:

298.000 
145.000 
275.000 
535.000 
487.000 
159.000 
325.000 
189.000 
139.000 
499.000 
520.000 
249.500 
448.000 
215.000 
225.000 
210.000 
215.000 
218.000 
232.000 
689.000 
228.000 
299.500 
169.000 
135.000 
549.000 
125.000 
160.000 
395.000 
430.000 
210.000 

【讨论】:

    【解决方案2】:

    这是您问题的示例代码。

    import re
    import requests
    page = requests.get("https://www.immoweb.be/en/search/apartment/for-sale/leuven/3000")
    print(page.content)
    
    from bs4 import BeautifulSoup
    soup = BeautifulSoup(page.content, 'html.parser')
    
    prices = []
    for items in soup.find_all("div", {"class": "xl-price rangePrice"}):
    if items.string:
        result = re.findall(r'\d+.\d+', items.string)
        prices.append(result[0])
    else:
        soup1 = BeautifulSoup(str(items), 'html.parser')
        for item in soup1.find("div", {"class": "xl-price rangePrice"}):
            if item.string:
                result = re.findall(r'\d+.\d+', item.string)
                if len(result)>0:
                    prices.append(result[0])
    
    print(prices)
    

    【讨论】:

    • 看起来代码跳过了具有“新/旧”价格的属性。知道如何让它在列表中包含新价格吗?
    • 程序已经使用 BeautifulSoup 时,使用 RegEx 有什么意义?
    • 根据问题,BeautifulSoup 给出的输出类似于 "\r\n\t\t\t\t\t\t\t\t298.000 € \r\n\t\t \t\t\t\t\t”。所以我使用正则表达式从字符串中提取数字。
    • @MoofinTheExplorer 我已经更新了答案,请查看。
    【解决方案3】:

    我现在无法使用计算机,因此请考虑以下准伪代码:

    new_price = div_elem.find(text=True, recursive=False)
    
    find_res = div_elem.find('span', attrs={'class': 'old-price'})
    
    if find_res:
        old_price = find_res.get_text(strip=True)
    

    我试图让事情尽可能容易理解。

    如果您有任何问题,请告诉我:)

    【讨论】:

    • @MoofinTheExplorer 好的,祈祷!明天我会回来,写一个更完整的解决方案。如果您也共享更多代码,这可能会很有用。
    • 嗨亚历山大!是的,确实如此,我很感激。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-14
    • 1970-01-01
    • 2017-07-27
    • 2011-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多