Python Scraping:如何从具有Multiple的类中返回单个值



我对使用Python很陌生。我正在丹麦一家股票交易网站上尝试使用BeautifulSoup来练习我的刮网技巧:https://www.nordnet.dk/markedet/aktiekurser

这是我的代码:

import requests
from bs4 import BeautifulSoup
page = requests.get('https://www.nordnet.dk/markedet/aktiekurser')
soup = BeautifulSoup(page.content, 'html.parser')
stocks = soup.find(id='tabs-tabpanel-0')
items = stocks.find_all(class_ ='c02356 c02375')
name = [item.find(class_ = 'c02393 c02394').get_text() for item in items]
Price = [item.find(class_ = 'number c02398').get_text() for item in items]
print(name)
print(Price)

它工作正常,但类"number c02398"似乎包含多个值,因为print(Price)返回两个值。如何更改代码,以便只从类中获得一个值?

这是在item变量中返回的span标记:

<span class="number c02398" style="white-space:nowrap"><span class="c02420"> <!-- -->426.2<!-- --> </span><span aria-hidden="true">426,20</span></span
  1. 如果要获取aria-hidden属性为True的span标记内的值,请使用以下代码行:
Price = [item.find(class_ = 'number c02398').find("span", attrs={"aria-hidden":"true"}).get_text() for item in items]

退货:

['426,20', '1.251,50', '13.035', '981,00', '337,30', '2.399', '600,00', '1.070,00', '925,00', '98,84', '178,50', '940,00', '12.250', '113,00', '229,00', '609,80', '560,00', '2.162', '492,50', '826,00', '198,35', '207,40', '264,40', '20,69', '56,30', '672,40', '223,50', '216,70', '183,40', '185,60', '255,40', '184,00', '469,50', '2.306', '53,04', '101,30', '1,125', '269', '126,10', '269,60', '229,20', '114,00', '541', '605', '24,16', '117,00', '114,80', '44,40', '78,80', '135', '71,80', '270,00', '126,20', '2,755', '12,20', '59,60', '2.040', '136,00', '116,00', '11,28', '219', '337,00', '110,00', '508', '49,05', '3,04', '5,24', '235', '87,40', '59,0', '4,90', '675', '57,50', '174,00', '47,15', '233', '10,20', '584', '1.015', '688', '118', '3,020', '4,12', '1,870', '248', '5,74', '5.200', '78,50', '4,92', '177,00', '0,393', '8,60', '15,50', '6.400', '448', '540', '13,95', '90,40', '9,43', '131,50']

  1. 如果要获取类名为c02420span标记内的值,请使用以下代码行:
Price = [item.find(class_ = 'number c02398').find("span").get_text() for item in items]

退货:

[' 426.2 ', ' 1251.5 ', ' 13035 ', ' 981 ', ' 337.3 ', ' 2399 ', ' 600 ', ' 1070 ', ' 925 ', ' 98.84 ', ' 178.5 ', ' 940 ', ' 12250 ', ' 113 ', ' 229 ', ' 609.8 ', ' 560 ', ' 2162 ', ' 492.5 ', ' 826 ', ' 198.35 ', ' 207.4 ', ' 264.4 ', ' 20.69 ', ' 56.3 ', ' 672.4 ', ' 223.5 ', ' 216.7 ', ' 183.4 ', ' 185.6 ', ' 255.4 ', ' 184 ', ' 469.5 ', ' 2306 ', ' 53.04 ', ' 101.3 ', ' 1.125 ', ' 269 ', ' 126.1 ', ' 269.6 ', ' 229.2 ', ' 114 ', ' 541 ', ' 605 ', ' 24.16 ', ' 117 ', ' 114.8 ', ' 44.4 ', ' 78.8 ', ' 135 ', ' 71.8 ', ' 270 ', ' 126.2 ', ' 2.755 ', ' 12.2 ', ' 59.6 ', ' 2040 ', ' 136 ', ' 116 ', ' 11.28 ', ' 219 ', ' 337 ', ' 110 ', ' 508 ', ' 49.05 ', ' 3.04 ', ' 5.24 ', ' 235 ', ' 87.4 ', ' 59 ', ' 4.9 ', ' 675 ', ' 57.5 ', ' 174 ', ' 47.15 ', ' 233 ', ' 10.2 ', ' 584 ', ' 1015 ', ' 688 ', ' 118 ', ' 3.02 ', ' 4.12 ', ' 1.87 ', ' 248 ', ' 5.74 ', ' 5200 ', ' 78.5 ', ' 4.92 ', ' 177 ', ' 0.393 ', ' 8.6 ', ' 15.5 ', ' 6400 ', ' 448 ', ' 540 ', ' 13.95 ', ' 90.4 ', ' 9.43 ', ' 131.5 ']

最新更新