【发布时间】:2019-05-14 12:26:52
【问题描述】:
我遇到了一个问题,即在没有获取所有 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()”函数,我仍然会得到这样的数据:
打印(文本) '49\n \n\n\n TEXT-I-WANT-TO-SCRAPE \n \n 1103'
我的问题是如何只获取标签内的文本?
同时抓取 elo 和排名是没有问题的,因为它们包含在具有特定类的 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)
最好的问候
【问题讨论】:
标签: python web-scraping beautifulsoup