我遇到了一个问题,在没有得到所有的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)
致以最诚挚的问候
使用
next_element
获取标签。试试看下面代码。已使用正则表达式来查找要废弃的特定href
。你知道吗输出:
相关问题 更多 >
编程相关推荐