【发布时间】:2020-07-18 02:53:05
【问题描述】:
我正在编写一个函数,该函数将进入团队 Transfermarket 页面,并将每年的 Page 中的表格中的所有数据带给我。
我遇到的问题有 2 个。
-
作为 market_value 的索引号 13 表示索引超出范围,但如果您打印 print('column:', len(all_td)) 的长度,您会得到 13,即 las 列。
-
我知道我可以做一个 drop_duplicates 但不想做的同一玩家获得五倍或五倍以上的结果。
我是这个领域的新手,这是我的课程,我被困在这里。
感谢您的帮助。
import requests
from bs4 import BeautifulSoup
import pandas as pd
from google.colab import drive
drive.mount('/content/drive')
data_CORIN = {
'name': [],
'field_position': [],
'date_of_birth': [],
'height': [],
'foot': [],
'market_value': [],
'anio': []
}
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/47.0.2526.106 Safari/537.36'}
l = [2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020]
for i in range(0,len(l)-1):
url = "https://www.transfermarkt.es/sport-club-corinthians-paulista/kader/verein/199/saison_id/{}/plus/1".format(l[i])
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.content, 'html.parser')
all_tr = soup.find_all('tr', {'class': ['odd', 'even']}, recursive=True)
print('rows:', len(all_tr))
for row in all_tr:
all_td = row.find_all('td', recursive=True)
print('columns:', len(all_td))
for column in all_td:
print(' >', column.text)
data_CORIN['name'].append( all_td[3].text.split('.')[0][:15])
data_CORIN['field_position'].append( all_td[4].text)
data_CORIN['date_of_birth'].append( all_td[5].text[12:14])
data_CORIN['height'].append( all_td[8].text )
data_CORIN['foot'].append( all_td[9].text )
data_CORIN['market_value'].append( all_td[12].text )
data_CORIN['anio'].append(l[i])
df = pd.DataFrame(data_CORIN)
print(df.head())
【问题讨论】:
标签: python web-scraping beautifulsoup