我在IDE(Pycharms Community Edition 2018.3.5 -x64)中运行Python 3。我已经在操作系统(Windows 7)上安装了python3。我正在尝试使用以下代码将网站上的数据导入.csv文件,并导致错误:
Traceback (most recent call last):
File "C:/Users/**usernamewhichIhaveommittedonpurpose**/PycharmProjects/untitled1/scraperInvestegate.py", line 10, in <module>
csv_writer = csv.writer(csv_file)
AttributeError: module 'csv' has no attribute 'writer'
请注意,我在家里的非工作笔记本电脑(macbook)上使用了与上述相同的IDE相同的代码,并且运行良好!
下面插入了代码,希望能对您有所帮助,因为在线似乎没有专门提到相同的情况,并且它可以在一台计算机上运行但不能在具有相同IDE的另一台计算机上运行很奇怪!:>
from bs4 import BeautifulSoup
import requests
import csv
source = requests.get('https://www.investegate.co.uk/').text
soup = BeautifulSoup(source, 'lxml')
csv_file = open('file.csv', 'w')
csv_writer = csv.writer(csv_file)
csv_writer.writerow(['list'])
for announcements in soup.find_all('a'):
list = announcements.text
print(list)
csv_writer.writerow(['list'])
csv_file.close()
编辑以下一条评论: 文件csv.py文件在运行代码之前已被更改,即使删除了csv.py文件且未通过python控制台使用(dir)找到csv.py文件,该文件也无法运行