import requests
from bs4 import BeautifulSoup
url = "https://www.reuters.com/finance/stocks/company-officers/MPAPss.BO"
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
table = soup.find('table', {'class': 'dataTable'})
for row in table.find_all("tr"):
for col in row.find_all("td"):
print(col.text)
它同时打印所有信息(名称,年份和名称)我想使用
在名称部分存储名称name = col[0].text
但它给出了我如何解决这个问题的错误?
答案 0 :(得分:0)
>>> for row in table.find_all("tr"):
... cols = row.find_all("td")
... if not cols:
... continue
... name, year, des = filter(None, [col.text.strip() for col in cols])
... print (f'Name: {name}, Year: {year}, Des: {des}')
...
Name: K. Ratna Prabha, Year: 2014, Des: Non-Executive Chairman of the Board
Name: Mohan Kulkarni, Year: 2012, Des: Compliance Officer, Company Secretary
Name: Naveen Singh, Year: 2014, Des: Managing Director, Executive Director
Name: B. Khanna, Year: 2014, Des: Non-Executive Director - Nominee of BIFR
Name: V. Rammohan, Year: 2013, Des: Non-Executive Director - Nominee of IFCI
Name: Aravind Shrivastava, Year: 2013, Des: Non-Executive Director
Name: M. Lakshminarayana, Year: 2009, Des: Non-Executive Independent Director
Name: C. Okaly, Year: 2010, Des: Non-Executive Independent Director
Name: S. Parameswarappa, Year: 1996, Des: Non-Executive Independent Director
Name: C. Shivashankar, Year: 1995, Des: Non-Executive Independent Director
>>>
答案 1 :(得分:0)
这是你想要给予atry的另一种方式:
Name: K. Ratna Prabha Year: 2014 Position: Non-Executive Chairman of the Board
Name: Mohan Kulkarni Year: 2012 Position: Compliance Officer, Company Secretary
Name: Naveen Singh Year: 2014 Position: Managing Director, Executive Director
Name: B. Khanna Year: 2014 Position: Non-Executive Director - Nominee of BIFR
Name: V. Rammohan Year: 2013 Position: Non-Executive Director - Nominee of IFCI
Name: Aravind Shrivastava Year: 2013 Position: Non-Executive Director
Name: M. Lakshminarayana Year: 2009 Position: Non-Executive Independent Director
Name: C. Okaly Year: 2010 Position: Non-Executive Independent Director
Name: S. Parameswarappa Year: 1996 Position: Non-Executive Independent Director
Name: C. Shivashankar Year: 1995 Position: Non-Executive Independent Director
输出:
UNION ALL