【发布时间】:2022-01-24 03:50:09
【问题描述】:
我尝试使用 python 网页抓取然后输出一个 csv 文件,但打印格式与 csv 格式不匹配。
输出enter image description here
如何打印这个预期结果? enter image description here
谢谢
下面是我的脚本
import urllib.request as req
import bs4
import csv
import pandas as pd
import re
from datetime import date, timedelta
def daterange(start_date, end_date):
for n in range(int((end_date - start_date).days)):
yield start_date + timedelta(n)
start_date = date(2021, 12, 10)
end_date = date(2021, 12, 15)
url="https://hkgoldprice.com/history/"
with open('gprice.csv','w',newline="") as f1:
for single_date in daterange(start_date, end_date):
udate = single_date.strftime("%Y/%m/%d")
urld = url + single_date.strftime("%Y/%m/%d")
writer=csv.writer(f1,delimiter = '\t',lineterminator='\n',)
writer.writerows(udate)
print(udate)
with req.urlopen(urld) as response:
data=response.read().decode("utf-8")
root=bs4.BeautifulSoup(data, "html.parser")
prices=root.find_all("div",class_="gp")
gshops=root.find_all("div",class_="gshop")
gpdate=root.find_all("div",class_="gp_date")
for price in prices:
print(price.text)
row = price
writer.writerows(row)
【问题讨论】: