表的美丽汤刮痕返回字符串列表而不是列表列表

时间:2019-10-04 16:18:18

标签: python web-scraping beautifulsoup

使用Beautiful Soup抓取网站将返回字符串列表,而不是列表列表。

我尝试了.replace,但是它无法循环处理列表。

import bs4 as bs
import urllib.request
import csv
import pandas as pd
source = urllib.request.urlopen('https://www.basketball-reference.com/players/d/duncati01/gamelog/2015').read()

soup = bs.BeautifulSoup(source,'lxml')

f = csv.writer(open('bball_ref.csv','w'))

body = soup.body    
table = soup.select('#pgl_basic')
table_rows = table[0].find_all('tr')

for tr in table_rows:
    td = tr.find_all('td')
    row = [i.text for i in td]

    f.writerow([row])

with open('bball_ref.csv') as csvfile:
    bball_ref = csv.reader(csvfile,delimiter=",")
    rows = []
    for row in bball_ref:
        rows.append(row)

1 个答案:

答案 0 :(得分:1)

row已经是一个列表,致电f.writerow()时不需要在其周围放置另一个列表。应该是

f.writerow(list)