Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 按文本值对元素进行排序_Python 3.x_Sorting_Beautifulsoup_Python Requests - Fatal编程技术网

Python 3.x 按文本值对元素进行排序

Python 3.x 按文本值对元素进行排序,python-3.x,sorting,beautifulsoup,python-requests,Python 3.x,Sorting,Beautifulsoup,Python Requests,我已经在互联网上搜索了关于如何做到这一点,但似乎有不同类型的问题和目标 我这里有汤。您可以对这段代码进行测试 from bs4 import BeautifulSoup html = ''' <table border="1" cellspacing="0" width="300"> <tbody><tr> <td width="50%"

我已经在互联网上搜索了关于如何做到这一点,但似乎有不同类型的问题和目标

我这里有汤。您可以对这段代码进行测试

from bs4 import BeautifulSoup

html = '''
<table border="1" cellspacing="0" width="300">
        <tbody><tr>
          <td width="50%"><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=1','Vocab','500','500',0)">lesson 1</a></td>
          <td><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=7','Vocab','500','500',0)">lesson 7</a></td>
        </tr>
        <tr>
          <td width="50%"><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=2','Vocab','500','500',0)">lesson 2</a></td>
          <td><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=8','Vocab','500','500',0)">lesson 8</a></td>
        </tr>
        <tr>
          <td width="50%"><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=3','Vocab','500','500',0)">lesson 3</a></td>
          <td><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=9','Vocab','500','500',0)">lesson 9</a></td>
        </tr>
        <tr>
          <td width="50%"><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=4','Vocab','500','500',0)">lesson 4</a></td>
          <td><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=10','Vocab','500','500',0)">lesson 10</a></td>
        </tr>
        <tr>
          <td width="50%"><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=5','Vocab','500','500',0)">lesson 5</a></td>
          <td><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=11','Vocab','500','500',0)">lesson 11</a></td>
        </tr>
        <tr>
          <td width="50%"><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=6','Vocab','500','500',0)">lesson 6</a></td>
          <td><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=12','Vocab','500','500',0)">lesson 12</a></td>
        </tr>
      </tbody></table>
'''


soup = BeautifulSoup(html,"html5lib")#use any parser you want.

for td in soup.find_all("td"):
    print(td)# This outputs the not sorted <td> tags

非常感谢你!非常感谢您的帮助。

这应该可以完成这项工作,并为您提供包含
a
标记的已排序
td
标记的列表

从bs4导入美化组
html=“”
'''
soup=BeautifulSoup(html,'html.parser')
#函数获取一个标记,找到它的子标记
#然后,它会找到其中的文本,然后将其拆分以获得数字
#然后将其作为int返回给排序函数
def分类汤(项目):
item=列表(item.children)[0]。文本
数据=项目。拆分(“”)
返回int(数据[1])
out=soup.findAll('td'))
out=已分拣(out,key=lambda元素:分拣(元素))
打印(输出)

Hello@Aasim,太棒了!谢谢!我想我应该用
汤。找到所有的
反正它们都一样,我想只是在版本上。谢谢!
<td width="50%"><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=1','Vocab','500','500',0)">lesson 1</a></td>
<td><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=7','Vocab','500','500',0)">lesson 7</a></td>
<td width="50%"><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=2','Vocab','500','500',0)">lesson 2</a></td>
<td><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=8','Vocab','500','500',0)">lesson 8</a></td>
<td width="50%"><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=3','Vocab','500','500',0)">lesson 3</a></td>
<td><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=9','Vocab','500','500',0)">lesson 9</a></td>
<td width="50%"><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=4','Vocab','500','500',0)">lesson 4</a></td>
<td><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=10','Vocab','500','500',0)">lesson 10</a></td>
<td width="50%"><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=5','Vocab','500','500',0)">lesson 5</a></td>
<td><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=11','Vocab','500','500',0)">lesson 11</a></td>
<td width="50%"><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=6','Vocab','500','500',0)">lesson 6</a></td>
<td><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=12','Vocab','500','500',0)">lesson 12</a></td>
<td width="50%"><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=1','Vocab','500','500',0)">lesson 1</a></td> 

<td width="50%"><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=2','Vocab','500','500',0)">lesson 2</a></td> 

<td width="50%"><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=3','Vocab','500','500',0)">lesson 3</a></td> 

<td width="50%"><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=4','Vocab','500','500',0)">lesson 4</a></td> 

<td width="50%"><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=5','Vocab','500','500',0)">lesson 5</a></td> 

<td width="50%"><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=6','Vocab','500','500',0)">lesson 6</a></td> 

<td><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=7','Vocab','500','500',0)">lesson 7</a></td> 

<td><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=8','Vocab','500','500',0)">lesson 8</a></td> 

<td><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=9','Vocab','500','500',0)">lesson 9</a></td> 

<td><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=10','Vocab','500','500',0)">lesson 10</a></td> 

<td><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=11','Vocab','500','500',0)">lesson 11</a></td> 

<td><a href="javascript:newDoWindowOpen('http://ohelo.org/japn/lang/genki_vocab_table.php?lesson=12','Vocab','500','500',0)">lesson 12</a></td>