猿问

python beatifulsoup webscrape循环中的不同元素

我想刮一张桌子上有一个循环


<ul>

<li class="cell036 tal arrow"><a href=" y/">ALdCTL</a></li>

<li class="cell009">5,71</li>

<li class="cell009">5,74</li>   

<li class="cell009">-3,04</li>   

<li class="cell009">5,92</li>   

<li class="cell009">5,76</li>   

<li class="cell009">5,53</li>   

<li class="cell009">907.438</li>   

<li class="cell009">5.114.192</li> 

</ul>

我的python代码用于在类a的第一个元素中查找文本而不是文本licell009


c=soup.findAll('li',class_='cell036 tal arrow' )


for foo in soup.find_all('li', class_= ['cell036 tal arrow']):


   bar = foo.find(['a'])

   print(bar.text)


紫衣仙女
浏览 174回答 3
3回答

largeQ

要抓取所有值,您只需要获取所有li标签(不限制搜索具有类的元素,cell036 tal arrow这就是为什么您只能获取该值):试试这个:from bs4 import BeautifulSouphtml_text = """<ul><li class="cell036 tal arrow"><a href=" y/">ALdCTL</a></li><li class="cell009">5,71</li><li class="cell009">5,74</li><li class="cell009">-3,04</li><li class="cell009">5,92</li><li class="cell009">5,76</li><li class="cell009">5,53</li><li class="cell009">907.438</li><li class="cell009">5.114.192</li></ul>"""soup = BeautifulSoup(html_text, "lxml")for foo in soup.find_all('li'):&nbsp; &nbsp;print(foo.text)输出:ALdCTL5,715,74-3,045,925,765,53907.4385.114.192

温温酱

其他li元素不包含里面的li你正在寻找之中。他们是兄弟姐妹。使用find_next_siblings:content = """<ul><li class="cell036 tal arrow"><a href=" y/">ALdCTL</a></li><li class="cell009">5,71</li><li class="cell009">5,74</li>&nbsp; &nbsp;<li class="cell009">-3,04</li>&nbsp; &nbsp;<li class="cell009">5,92</li>&nbsp; &nbsp;<li class="cell009">5,76</li>&nbsp; &nbsp;<li class="cell009">5,53</li>&nbsp; &nbsp;<li class="cell009">907.438</li>&nbsp; &nbsp;<li class="cell009">5.114.192</li>&nbsp;</ul>"""soup = bs4.BeautifulSoup(content)header = soup.findAll("li", class_="cell036 tal arrow")header[0].find_next_siblings("li")给出:[<li class="cell009">5,71</li>,&nbsp;<li class="cell009">5,74</li>,&nbsp;<li class="cell009">-3,04</li>,&nbsp;<li class="cell009">5,92</li>,&nbsp;<li class="cell009">5,76</li>,&nbsp;<li class="cell009">5,53</li>,&nbsp;<li class="cell009">907.438</li>,&nbsp;<li class="cell009">5.114.192</li>]
随时随地看视频慕课网APP

相关分类

Python
我要回答