列表不允许.splitlines() - Python

时间:2015-10-01 21:48:37

标签: python csv web-scraping beautifulsoup

我需要做些什么来防止错误:AttributeError: 'list' object has no attribute 'split lines'在这里发生?如何将我拥有的列表转换为可以将splitlines归因于?

的表单
import requests
import re
from bs4 import BeautifulSoup
import csv

#Read csv
with open ("gyms4.csv") as file:
    reader = csv.reader(file)
    csvfilelist = [row[0] for row in reader]

print csvfilelist

#Get data from each url
def get_page_data():
    for page_data in csvfilelist.splitlines():
        r = requests.get(page_data.strip())
        soup = BeautifulSoup(r.text, 'html.parser')
        yield soup

1 个答案:

答案 0 :(得分:2)

str.splitlines() method仅适用于字符串对象。你没有字符串对象,你有一个字符串列表:

csvfilelist = [row[0] for row in reader]

无需拆分,您已经拥有文件中每行的第一列。只需删除.splitlines()电话:

即可
for page_data in csvfilelist: