问题
What do I need to do to prevent the error: AttributeError: 'list' object has no attribute 'split lines' from occurring here? How to I convert the list that I have into a form that can have splitlines attributed to?
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:
The str.splitlines() method only works on a string object. You don't have a string object, you have a list of strings:
csvfilelist = [row[0] for row in reader]
There is no need to split this, you already have the first column of each line in the file. Just remove the .splitlines() call:
for page_data in csvfilelist:
来源:https://stackoverflow.com/questions/32897405/list-not-allowing-splitlines-python