Python 使用BeautifulSoup获取HTML标记

Python 使用BeautifulSoup获取HTML标记,python,html,beautifulsoup,Python,Html,Beautifulsoup,我正在使用Beauty soup从这里删除数据: 这是我在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='na

我正在使用Beauty soup从这里删除数据:

这是我在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'
导入请求
从bs4导入BeautifulSoup
第页=”https://www.booli.se/annons/2272818"
request=requests.get(第页)
soup=BeautifulSoup(request.text,'lxml')
Int[3]:soup.findAll('span',itemprop='name')[4].text.strip().encode('utf-8'))
输出[3]:'3朗姆酒,67米\xc2\xb2'
Int[4]:d=soup.findAll('span',class='property'\uuuu base-info\uuu value')
输出[4]:[\n\t\t\t17年3月
2017年\n\t\t,
\n\t\t\t2 850 000
kr\n\t\t\t42 537
kr/m\xb2\n,
4921 kr/m\xe5n,
L\xe4genhet,
233 kr/m\xe5n,
3 tr,
1907 ]
Int[5]:d[2].text.strip().encode('utf-8')
Out[5]:'4921 kr/m\xc3\xa5n'
Int[6]:d[1].text.strip().encode('utf-8')
输出[6]:'2850000 kr\n\t\t\t42 537 kr/m\xc2\xb2'
现在我的问题是: Q1:In-Out[3]->如何从67M\xc2\xb2中分离出3个朗姆酒? Q2:In-Out[3]->我如何摆脱朗姆酒和m\xc2\xb2?我只想保存3个房间和67个面积,如: 房间面积 367


问题3:同样的问题。我需要删除文本并单独保存值。很抱歉,该网页是瑞典语的。谢谢。

m\xc2\xb2
是打印出来的,因为它无法在UTF-8中理解

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

soup.findAll('span',itemprop='name')[4]。文本
它打印
3朗姆酒,67平方米

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

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)

我现在有一份口述。太好了。谢谢:-)@Mary刚刚添加了一些更改,可能会更有用