TL; DR我需要將BS4結果集列表(單列)變成NxN數組,但怎麼做?我怎樣才能得到連接標題,也是BS4結果集列表?下面的代碼。謝謝!將BS4結果集轉換爲相對於頭部的NxN數組(單獨的BS4結果集)
所以我試圖網絡刮運動數據,但我遇到麻煩將結果集轉換爲NxN數組。另外,我試圖包含以相同的方式刮掉的頭文件。這裏是我到目前爲止的代碼:
import requests
from bs4 import BeautifulSoup
from __future__ import print_function
import numpy as np
url=input("Paste player link and specific year ")
r= requests.get(url)
html_content=r.text
soup=BeautifulSoup(html_content,"lxml")
body = soup.body
table=body.table
tbody=table.tbody
headers = table.find_all("th")
statistics = tbody.find_all("td")
def string_stats():
for stat in statistics:
print (stat.string)
def string_headers():
for head in headers:
print (head.string)
string_stats_list = string_stats()
string_stats_list
這導致在短短的td標籤元素串的垂直列表(或者說是目標)。
所以,我的問題是:我怎樣才能得到這個單列列表到一個NxN數組/矩陣?另外,我怎樣才能連接標題?
感謝您的閱讀和/或幫助!
請給真正的鏈接這是您目前解析 –
http://www.footballdb.com/players/mike-evans-evansmi03/gamelogs 感謝您! –