【发布时间】:2021-12-06 05:17:51
【问题描述】:
我是 Python 中 Pandas 和 Webscraping 和 BeautifulSoup 的新手。
当我学习通过使用requests 和BeautifulSoup scrape 网页来使用 Python 进行一些基本的网页抓取时,我对分配网页的第二个和第三个元素的任务感到困惑将 html 表转换为 pandas 数据框。
到目前为止,这是我的代码:
import pandas as pd
from bs4 import BeautifulSoup
import requests
html_data = requests.get('https://en.wikipedia.org/wiki/List_of_largest_banks').text
soup = BeautifulSoup(html_data, 'html.parser')
data = pd.DataFrame(columns=["Name", "Market Cap (US$ Billion)"])
for row in soup.find_all('tbody')[3].find_all('tr'): #This line will make sure to get to the third table which is this "By market capitalization" table on the webpage and finding all the rows of this table
col = row.find_all('td') #This is to target individual column values in a particular row of the table
for j, cell in enumerate(col):
#Further code here
可以看出,我想定位一行的所有第 2 和第 3 列值并附加到
空数据框data,以便data 包含银行名称和市值值。我怎样才能实现这种功能?
【问题讨论】:
标签: python pandas beautifulsoup python-requests