我需要网站刮网址,并保存到excel像我上传的图像
但我不知道我的代码怎么了
我的excel文件中只有一行。请帮帮我。你知道吗
import requests
from bs4 import BeautifulSoup
import csv
for i in range(10):
payload={'pageIndex':i}
r=requests.post(url, params=payload)
soup=BeautifulSoup(r.text, 'html.parser')
table=soup.find('table')
rows=table.find('tbody').find_all('tr')
for j in range(len(rows)):
col=rows[j].find_all('td')
result=[]
for item in col:
result.append(item.get_text())
with open(r"C:\Users\lwt04\Desktop\TheaterInfo.csv","w",newline='') as out:
theater = csv.writer(out)
with open(r"C:\Users\lwt04\Desktop\TheaterInfo.csv","a",newline='') as out:
theater = csv.writer(out)
theater.writerow(result)
您的代码只存储最后一个剧院-这是一个逻辑错误。您需要将每个剧院
result
行存储在所有theaters
的列表中,并将其写入文件:如果您想附加,或者创建look into Check a file exists or not without try-catch block,并考虑将opening mode设置为变量
'w'
或'a'
,具体取决于文件是'w'
还是写头,否则只写数据。你知道吗附录-您不是在写excel,而是在写一个可以由excel打开的CSV文件。要直接编写excel,请使用适当的模块,如f.e.此处:https://openpyxl.readthedocs.io/en/stable/
HTH公司
将
results
保存到另一个列表,并将该列表写入csvfile。你知道吗您也可以使用
pandas
来实现这个目的。你只需要为result
做些什么。你知道吗对于CSV
您可以简单地对
result
使用它,因为它只是数据的一行。如果你想做listofresult
,对于多个条目都可以处理。你知道吗相关问题 更多 >
编程相关推荐