我在python中编写了一个刮刀,用于解析网页中的不同名称。因为我对熊猫来说很新,所以不能理解如何正确地在csv文件中写入数据。我编写脚本的方式确实刮掉了所需的名称,但是如果写入csv文件,它只会写出姓氏。对此的任何帮助将非常感激。提前谢谢。
import requests ; from bs4 import BeautifulSoup
import pandas as pd
res = requests.get('https://bytes.com/topic/python/answers/596974-socket-error-10061-connection-refused').text
soup = BeautifulSoup(res,"lxml")
for name in soup.select(".smallfont a"):
item = pd.DataFrame([name.text])[0]
item.to_csv('file.csv')
print(item)
答案 0 :(得分:1)
我不确定这是不是你的问题,但问题是你在覆盖循环中的数据帧。这样,只有列表中的最后一个数据将记录在csv文件中。
试试这段代码:
import requests ; from bs4 import BeautifulSoup
import pandas as pd
res = requests.get('https://bytes.com/topic/python/answers/596974-socket-
error-10061-connection-refused').text
soup = BeautifulSoup(res,"lxml")
items_list = []
for name in soup.select(".smallfont a"):
items_list.append(name.text)
item = pd.DataFrame(items_list)
item.to_csv('file.csv')
print(item)
Out put:
0
0 boost::asio (Permission denied, Connection ref...
1 Error Code 10061: Connection refused
2 socket.error connection refused
3 Connection refused error from browser
4 connection refused when uploading a package to...
5 smtplib (111, 'Connection refused')
6 sending of mail (smtp) - connection refused - ...
7 socket object, connection refused ...
8 socket error 10061
这是你的意图吗?
答案 1 :(得分:0)
你为什么需要熊猫?
with open('file.csv', 'w+') as f:
for name in soup.select(".smallfont a"):
item = name.decode_contents()
f.write(item)
print(item)