我在使用Pandas DataFrame时遇到空的SFMA.csv输出,同时如果我打印内容,代码也能正常工作。代码如下:
import csv
import urllib
import requests
from bs4 import BeautifulSoup
import time
import datetime
from random import randint
import numpy as np
import pandas as pd
fi = open('Input_List.csv', 'r')
#readers = list(csv.reader(fi))
readers=csv.reader(fi)
#print(readers)
df = []
for reader in readers:
#print(str(reader)[1:-1]+"\n")
url=str(reader)[2:-2]
request = requests.session()
htmlpage = requests.get(url)
#print("status code: "+ str(htmlpage.status_code))
if htmlpage.status_code != 200 :
break # something went wrong #
soup = BeautifulSoup(htmlpage.text,'lxml')
for result_table in soup.find_all("div", {"class": "w3-container"}):
#content=result_table.find('p')
#print(result_table)
content=result_table.text
if(content.find("Website") > -1):
index=content.find("Website")
content=content[:content.find("\n",index)]
#print(content)
df=np.append(df,content)
break
#print(content)
df=np.append(df,str(content))
#print(df)
df1 = pd.DataFrame(df)
df1.to_csv("SFMA.csv",index=False,encoding='utf-8')
fi.close()
打印时输出
答案 0 :(得分:0)
可以通过这种方式读取文件:
df = pd.read_csv('C:/Users/tk/Input_List.csv', header=0,encoding = 'unicode_escape')