对于BS中返回的ResultSet的使用心得

初次使用python进行爬虫学习,之前一直被卡在BS中find_all()这个函数的使用上,今天无意中调通,发现原来find_all()返回的是一个ResultSet的集合,而这个集合可以视为一种list,只不过其中len()=1,因此对于不断缩小片段而言,是非常好用的。废话少说,上代码


soup = BeautifulSoup(html, "html.parser")
sInfo = soup.find_all('div', attrs={'class': 'stock-bets'})
name = sInfo[0].find('a', attrs={'class': 'bets-name'}).text.split()
这其中,soup是BS搜寻中返回的网页代码树,由于其体积相对比较庞大,因此可以对其中片段进行缩小,因此其中就可以反复使用findall()函数,只不过,必须将其指定到具体位置,即要使用【0】,代码中使用sInfo[0].find all()




你可能感兴趣的:(Python)