分割来自爬取数据中的html(Python+BeautifulSoup4)

2024-04-24 10:34:59 发布

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

我遇到了一个问题,在没有得到所有的html数据的情况下,在标签中抓取文本。 这是我的python代码。我要刮取的文本不在span类中,而是独立于标记中。下面是文本放置位置的示例。你知道吗

<a href="/counterstrike/rankings/team-details/32537">
  <span class="ranking">49</span>
  <span class="flag flag-pl" data-tooltip="" tabindex="1" title="Poland></span>
  TEXT-I-WANT-TO-SCRAPE
  <span class="elo">1103</span>
</a>

如果我用的是“。text.encode编码('utf8').lstrip().rstrip()“函数,我仍然得到如下数据:

print(textt) '49\n \n\n\n TEXT-I-WANT-TO-SCRAPE \n \n 1103'

我的问题是如何只获取标签内的文本?

刮去elo和ranking是没有问题的,因为它们包含在具有特定类的span中。你知道吗

def get_matches():
matches = get_parsed_page("https://www.gosugamers.net/counterstrike/rankings")
rankings = matches.find("ul", {"class": "ranking-list"})
matchdays = rankings.find_all("li")

for match in matchdays:
    matchDetails = match.find_all("a")

    for getMatch in matchDetails:
        elo = match.find("span", {"class": "elo"}).text.encode('utf8').lstrip().rstrip()
        ranking = match.find("span", {"class": "ranking"}).text.encode('utf8').lstrip().rstrip()
        textt = match.find("a").text.encode('utf8').lstrip().rstrip()

        print(ranking,elo,textt)

致以最诚挚的问候


Tags: text文本matchutf8findclassencodespan
1条回答
网友
1楼 · 发布于 2024-04-24 10:34:59

使用next_element获取标签。试试看下面代码。已使用正则表达式来查找要废弃的特定href。你知道吗

from bs4 import BeautifulSoup
import requests
import re
data=requests.get("https://www.gosugamers.net/counterstrike/rankings").text
soup=BeautifulSoup(data,'html.parser')
for a in soup.find_all('a',href=re.compile("/counterstrike/rankings/team-details")):
    ranking=a.find('span' , class_='ranking').text.replace('\n','').strip()
    name=a.find('span', class_='ranking').next_element.next_element.next_element.next_element.replace('\n','').strip()
    elo=a.find('span',class_='elo').text.replace('\n','').strip()
    print(ranking,name,elo)

输出:

1 Astralis 1505
2 Team Liquid 1469
3 ENCE eSports 1402
4 Vitality 1365
5 AVANGAR 1326
6 Natus Vincere 1298
7 Ninjas in Pyjamas 1294
8 fnatic 1292
9 MiBR 1269
10 FURIA 1264
11 mousesports 1258
12 Renegades 1252
13 NRG eSports 1248
14 ORDER 1240
15 Grayhound Gaming 1237
16 Valiance 1235
17 Windigo 1228
18 FaZe Clan 1222
19 North 1220
20 G2 Esports 1213
21 OpTic Gaming 1201
22 MVP PK 1196
23 Heroic 1183
24 Chiefs eSports Club 1177
25 3DMAX.CS 1173
26 HellRaisers 1168
27 Rogue 1167
28 BIG 1165
29 forZe 1165
30 Ghost Gaming 1159
31 Swole Patrol 1154
32 TyLoo 1151
33 Red Reserve 1142
34 Isurus Gaming 1142
35 Team Kinguin 1136
36 Tainted Minds 1135
37 Movistar Riders 1134
38 NoChance 1134
39 DETONA Gaming 1132
40 Space Soldiers 1120
41 Bravado Gaming 1117
42 BPro Gaming 1116
43 Cloud9 1116
44 GamerLegion 1113
45 CyberZen 1111
46 Epsilon 1111
47 CLG Red 1107
48 Luminosity Gaming 1107
49 devils.one 1103
50 Sprout 1096

相关问题 更多 >