使用BeautifulSoup获取HTML标记

2024-04-25 13:28:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用beautiful soup从这里删除数据:https://www.booli.se/annons/1887654

这是我在python 2.7中的代码:

import requests
from bs4 import BeautifulSoup 
page="https://www.booli.se/annons/2272818"
request = requests.get(page)
soup = BeautifulSoup(request.text,'lxml') 


Int[3]: soup.findAll('span', itemprop='name')[4].text.strip().encode('utf-8')

Out[3]:'3 rum, 67 m\xc2\xb2'


Int[4]:d=soup.findAll('span', class_='property__base-info__value')

Out[4]: [<span class="property__base-info__value">\n\t\t\t17 mar
       2017\n\t\t</span>,
     <span class="property__base-info__value">\n\t\t\t2 850 000    
      kr\n\t\t\t<span class="property__base-info__sub-value">42 537    
      kr/m\xb2</span>\n</span>,
    <span class="property__base-info__value">4 921 kr/m\xe5n</span>,
    <span class="property__base-info__value">L\xe4genhet</span>,
    <span class="property__base-info__value">233 kr/m\xe5n</span>,
   <span class="property__base-info__value">3 tr</span>,
    <span class="property__base-info__value">1907 </span>]

  Int[5]: d[2].text.strip().encode('utf-8')
  Out[5]:'4 921 kr/m\xc3\xa5n'


 Int[6]: d[1].text.strip().encode('utf-8')
 Out[6]: '2 850 000 kr\n\t\t\t42 537 kr/m\xc2\xb2'

现在我的问题是: 问题1:输入输出[3]--->;如何将3朗姆酒从67 m\xc2\xb2中分离出来? 问题2:输入输出[3]--->;如何去除朗姆酒和m\xc2\xb2?我只想保存3个房间和67个面积,如: 房间面积 3 67个

问题3:同样的问题。我需要删除文本,只需单独保存值。抱歉,网页是瑞典语的。非常感谢。你知道吗


Tags: textinfobasevaluepropertyoutclassencode
1条回答
网友
1楼 · 发布于 2024-04-25 13:28:28

打印m\xc2\xb2是因为它无法理解UTF-8中的。你知道吗

如果将soup.findAll('span', itemprop='name')[4].text.strip().encode('utf-8')更改为

soup.findAll('span', itemprop='name')[4].text它打印3 rum, 67 m²

如果您确信源具有一致的格式,并且只需要数字值,则可以执行以下操作:

import requests
from bs4 import BeautifulSoup
page="https://www.booli.se/annons/2272818"
request = requests.get(page)
soup = BeautifulSoup(request.text,'lxml')

info = {}
a = soup.findAll('span', itemprop='name')[4].text
a = [int(s) for s in a.split() if s.isdigit()]
info['Rooms'] = a[0]
info['Area'] = a[1]
temp = []
date = soup.findAll('span', class_='property__base-info__value')
for i in date:
    i = i.text.strip()
    temp.append(i)

temp[1] = temp[1].split('\n')[0]

info['Såld'] = temp[0]
info['Utropspris'] = temp[1]
info['Avgift'] = temp[2]
info['Bostadsty'] = temp[3]
info['Driftskostnad'] = temp[4]
info['Våning'] = temp[5]
info['Byggår'] = temp[6]
import pprint
pprint.pprint(info)

相关问题 更多 >

    热门问题