问题描述
假设我有一个包含 10 列和 100 行的 html 表,我想要做的就是在退出时使用 Beautifulsoup 查找数据并打印整行。
import urllib3
from bs4 import BeautifulSou
http = urllib3.PoolManager()
url "https://en.wikipedia.org/wiki/List_of_Asian_countries_by_area"
response = http.request('GET', url)
soup = BeautifulSoup(response.data, "html.parser")
for tr in soup.find_all('tr')[2:]:
tds = tr.find_all('td')
print(tds)
1楼
编辑:
import urllib3
import re
from bs4 import BeautifulSoup
def searchTableForCountry(country):
outlist = []
http = urllib3.PoolManager()
url = "https://en.wikipedia.org/wiki/List_of_Asian_countries_by_area"
response = http.request('GET', url)
soup = BeautifulSoup(response.data, "html.parser")
for td in soup.find_all('tr'):
if(len(td.find_all('a', {'href': re.compile(r'/wiki/' + country)})) >=1):
outlist.append(td.text.replace('\n',' ').replace('\xa0',' '))
print(''.join(outlist))
return outlist
searchTableForCountry('China')
#2 China 9,596,961 excludes Hong Kong, Macau, Taiwan and disputed areas/islands