0

假设我有一个包含 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)
4

1 回答 1

0

编辑:

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 
于 2020-03-22T02:09:02.400 回答