在BeautifulSoup响应中处理十六进制值吗?

问题描述 投票:0回答:2

我正在使用漂亮的汤来抓取一些数据:

url = "https://www.transfermarkt.co.uk/jorge-molina/profil/spieler/94447"
heads = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36'}

response = requests.get(url,headers=heads)
soup = BeautifulSoup(response.text, "lxml")

然后,我提取特定的信息:

height = soup.find_all("th", string=re.compile("Height:"))[0].findNext("td").text
print(height)

按预期工作,打印

1,74 m

但是当我尝试使用此函数评估该字符串时:

def format_height(height_string):
    return int(height_string.split(" ")[0].replace(',',''))

我收到以下错误:

format_height(height)
Traceback (most recent call last):
  File "get_player_info.py", line 73, in <module>
    player_info = get_player_info(url)
  File "get_player_info.py", line 39, in get_player_info
    format_height(height)
  File "/Users/kompella/Documents/la-segunda/util.py", line 49, in format_height
    return int(height_string.split(" ")[0].replace(',',''))
ValueError: invalid literal for int() with base 10: '174\xa0m'

我想知道如何评估自己得到的十六进制值?

python-3.x beautifulsoup hex
2个回答
0
投票

一切都很好,只需解构它们,然后您就可以做任何您想做的事。

import requests
import re
from bs4 import BeautifulSoup

url = "https://www.transfermarkt.co.uk/jorge-molina/profil/spieler/94447"
heads = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36'}

response = requests.get(url,headers=heads)
soup = BeautifulSoup(response.text, "lxml")

height = soup.find_all("th", string=re.compile("Height:"))[0].findNext("td").text
numerals = [int(s) for s in re.findall(r'\b\d+\b', height)]
print (numerals)
#output: [1, 74]
print ("Height is: " + str(numerals[0]) +"."+ str(numerals[1]) +"m")
#output: Height is: 1.75m
print ("Height is: " + str(numerals[0]) + str(numerals[1]) +"cm")
#output: Height is: 175cm

无论如何,在此主题中讨论了相同的问题。您可以看一下:ValueError: invalid literal for int() with base 10: ''


0
投票

使用属性=值选择器定位目标高度,然后按原样使用函数

import requests
from bs4 import BeautifulSoup as bs

def format_height(height_string):
    return int(height_string.split(" ")[0].replace(',',''))

r = requests.get('https://www.transfermarkt.co.uk/jorge-molina/profil/spieler/94447', headers = {'User-Agent':'Mozilla\5.0'})
soup = bs(r.content,'lxml')
height_string = soup.select_one('[itemprop=height]').text

print(format_height(height_string))
© www.soinside.com 2019 - 2024. All rights reserved.