我有一个文本文件,里面填充了我需要抓取的链接。每个网站都有一个表,我需要获取并保存在自己的CSV文件中,我有这个代码:
from bs4 import BeautifulSoup
import pandas as pd
import csv
import urllib2
def stir_the_soup():
player_links = open('player_links.txt', 'r')
player_ID_nums = open('player_ID_nums.txt', 'r')
id_nums = [x.rstrip('\n') for x in player_ID_nums]
idx = 0
for url in player_links:
player_link = urllib2.urlopen(url)
bs = BeautifulSoup(player_link, 'lxml')
table_id = ""
if url[-12] == 'b':
table_id = "batting"
elif url[-12] == 'p':
table_id = "pitching"
table = str(bs.find('table', {'id' : (table_id + '_gamelogs')}))
df = pd.read_html(table)
df2 = df[idx]
df2.to_csv(path_or_buf=("%s.csv", id_nums[idx]), sep=',')
idx += 1
if __name__ == "__main__":
stir_the_soup()
id_nums列表是我想要的每个CSV文件的名称列表。
但是我收到了这个错误:
AttributeError: 'tuple' object has no attribute 'close'
不知道出了什么问题,我知道read_html()会保存一组数据帧,因此我试图获取每个数据帧并将其保存到自己的CSV中。我四处搜寻,找不到合适的答案。任何帮助表示赞赏
答案 0 :(得分:1)
据我所见,您将元组传递给 path_or_buf 属性。这会引发AttributeError。更改','代表'%'可以解决您的问题:
self.IPAddress_comboBox.currentIndexChanged['QString'].connect(self.handleChanged)
def handleChanged(self, text):
index= self.IPAddress_comboBox.findText(text,QtCore.Qt.MatchFixedString)
if index >=0:
self.IPAddress_comboBox.setCurrentIndex(index)