如何使用beatifulsoup python提取表列和行

如何使用beatifulsoup python提取表列和行,python,beautifulsoup,Python,Beautifulsoup,这里有个漂亮的地方。只是为了练习,我试图提取这里的包和版本列。我尝试使用:table=soup.find('table',attrs={'class':'listing sortable'})获取表内容,但我没有真正获得任何有价值的数据。。我很迷路 这是截图 我不确定你现在得到了什么结果,但是试试这个 import requests import bs4 url = 'https://launchpad.net/~openshot.developers/+archive/ubuntu/ppa

这里有个漂亮的地方。只是为了练习,我试图提取这里的包和版本列。我尝试使用:
table=soup.find('table',attrs={'class':'listing sortable'})
获取表内容,但我没有真正获得任何有价值的数据。。我很迷路

这是截图

我不确定你现在得到了什么结果,但是试试这个

import requests
import bs4

url = 'https://launchpad.net/~openshot.developers/+archive/ubuntu/ppa'
res = requests.get(url)
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text, "html.parser")
tbody = soup.find_all(id='packages_list')[0].tbody

for tr in tbody.find_all('tr'):
    package = tr.find_all('td')[0].contents[2].strip()
    version = tr.find_all('td')[1].contents[0].strip()
    print('{0} - {1}'.format(package, version))

我不确定你现在得到了什么结果,但是试试这个

您可以迭代
tr
标记并提取包和版本:

import requests
import bs4

url = 'https://launchpad.net/~openshot.developers/+archive/ubuntu/ppa'
res = requests.get(url)
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text, "html.parser")
tbody = soup.find_all(id='packages_list')[0].tbody

for tr in tbody.find_all('tr'):
    package = tr.find_all('td')[0].contents[2].strip()
    version = tr.find_all('td')[1].contents[0].strip()
    print('{0} - {1}'.format(package, version))
table = soup.find('table', attrs={'class': 'listing sortable'})
package = '' ; version = ''
for i in table.select('tr'):
    data = i.select('td')
    if data:
        package = data[0].text.strip()
        version = ' '.join(data[1].text.strip().split())
        print('{} : {} '.format(package,version))

#output
libopenshot : 0.1.4+0+588+107+201703310338+daily~ubuntu17.04.1 
libopenshot : 0.1.4+0+588+107+201703310338+daily~ubuntu15.04.1 
libopenshot : 0.1.4+0+588+107+201703310337+daily~ubuntu16.10.1 
libopenshot : 0.1.4+0+588+107+201703310337+daily~ubuntu16.04.1 
...
...

您可以迭代
tr
标记并提取包和版本:

table = soup.find('table', attrs={'class': 'listing sortable'})
package = '' ; version = ''
for i in table.select('tr'):
    data = i.select('td')
    if data:
        package = data[0].text.strip()
        version = ' '.join(data[1].text.strip().split())
        print('{} : {} '.format(package,version))

#output
libopenshot : 0.1.4+0+588+107+201703310338+daily~ubuntu17.04.1 
libopenshot : 0.1.4+0+588+107+201703310338+daily~ubuntu15.04.1 
libopenshot : 0.1.4+0+588+107+201703310337+daily~ubuntu16.10.1 
libopenshot : 0.1.4+0+588+107+201703310337+daily~ubuntu16.04.1 
...
...