简体   繁体   English

如何从表中获取特定变量中的特定信息

[英]how to get particular information in particular variable from table

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)

it prints the all information simultaneously(name,year and designation) i want to store name in name section using 它同时打印所有信息(名称,年份和名称),我想使用

name = col[0].text

but it gives the the error how i solve this? 但是它给出了错误我该如何解决?

>>> 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
>>> 

This is another way you might wanna give atry: 这是您可能想提出的另一种方式:

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")[1::1]:
    col = row.find_all("td")
    name = col[0].get_text(strip=True)
    year = col[2].get_text(strip=True)
    position = col[3].get_text(strip=True)
    print("Name: {} Year: {} Position: {}".format(name,year,position))

Output: 输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM