我正在尝试从链接解析表。我尝试过:
from bs4 import BeautifulSoup
import requests
url = 'http://www.stats.gov.cn/tjsj/zxfb/201810/t20181015_1627579.html'
page = requests.get(url)
soup = BeautifulSoup(page.text, 'html.parser')
for table in soup.find_all(class_='MsoNormalTable'):
print(table)
但无法获取因此,您能指导我,如何使用python解析表。
答案 0 :(得分:0)
您可以尝试:
soup.find_all("table", {"class": "MsoNormalTable"})
您应该指定标签,并且要通过属性访问,应在字典中传递
答案 1 :(得分:0)
您能这样做吗?我看不懂该语言,但可能是正确的。
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
url = 'http://www.stats.gov.cn/tjsj/zxfb/201810/t20181015_1627579.html'
page = requests.get(url, headers=headers)
soup = BeautifulSoup(page.text, 'lxml')
middleTable = soup.find('table', class_='MsoNormalTable')
rows = middleTable.findAll('tr')
for eachRow in rows:
print(eachRow.text)