2017-04-25 42 views

回答

2
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)) 
1
table = soup.find("table", id="packages_list") 
row_data = [] 
for row in table.find_all("tr"): 
    cols = row.find_all("td") 
    cols = [ele.text.strip() for ele in cols] 
    row_data.append(cols) 

我不知道是什麼結果你得到的權利,但嘗試了這一點!

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 
... 
... 
相關問題