从抓取的数据中分割html(Python + BeautifulSoup4)

hojlund123

我在抓取标签内的文本而没有获取所有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-SCRAPE \ n \ n 1103'

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

刮除elo和rank都是没有问题的,因为它们包含在具有特定类的范围内。

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)

最好的祝福

昆杜克

使用next_element得到下面code.Used正则表达式tag.Try的下一个元素的文本来找到特定的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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用 Python 抓取 HTML 中的特定元素:BeautifulSoup4

无法使用BeautifulSoup4抓取网站

使用BeautifulSoup4解析数据

使用Beautifulsoup4从HTML剥离Doctype?

用BeautifulSoup4解析HTML表

Python BeautifulSoup4 不嵌套/迭代

Python Beautifulsoup4解析多个表

BeautifulSoup4无法从表中抓取数据

使用BeautifulSoup4进行数据抓取的问题

BeautifulSoup4无法从此表中抓取数据

使用BeautifulSoup4在Python中存储标签中的数据

使用beautifulsoup4,Python在html标签内查找链接

使用beautifulsoup4后如何分离抓取结果?

使用python + beautifulSoup4从动态图中抓取数据

使用python和Beautifulsoup4从抓取数据中写入和保存CSV文件

如何使用python和beautifulsoup4循环抓取网站中多个页面的数据

循环不适用于使用python和beautifulsoup4抓取数据

BeautifulSoup4 从 pre 样式中提取和选择数据

使用beautifulsoup4从网站提取数据并解析成csv

是否可以在BeautifulSoup4中遍历HTML树?

未使用beautifulsoup4定义的名称错误'html'

使用 python beautifulsoup4 查找以 .rss 结尾的 url

无法在Mac OS上的python中安装beautifulsoup4

Python请求和beautifulsoup4,仅收集“ href”链接

从此元素获取链接| python3 BeautifulSoup4

Python beautifulsoup4 libary find_all()函数问题

如何忽略BeautifulSoup4 Python上的标签

Python BeautifulSoup4 WebCrawler .findAll() 不解析

BeautifulSoup4抓取不能超过网站的第一页(Python 3.6)