python-3.x - 使用 BeautifulSoup 3 将数据从 json 格式提取到 csv 的问题
问题描述
我正在尝试以 CSV 格式从 json 格式导出数据,但没有得到任何结果。下面是代码
import requests
from bs4 import BeautifulSoup
import json
import re
url = "https://www.daraz.pk/catalog/?q=dell&_keyori=ss&from=input&spm=a2a0e.home.search.go.35e34937qjElRf"
page = requests.get(url)
print(page.status_code)
print(page.text)
soup = BeautifulSoup(page.text, 'html.parser')
print(soup.prettify())
alpha = soup.find_all('script',{'type':'application/ld+json'})
jsonObj =`json.loads(alpha[1].text)`
for item in jsonObj['itemListElement']:
name = item['name']
price = item['offers']['price']
currency = item['offers']['priceCurrency']
availability = item['offers']['availability'].split('/')[-1]
availability = [s for s in re.split("([A-Z][^A-Z]*)", availability) if s]
availability = ' '.join(availability)
print('Availability: %s Price: %0.2f %s Name: %s' %(availability,float(price), currency,name))
这是我尝试以 CSV 格式导出数据但未以 CSV 格式获取结果的代码
创建要写入的文件,添加标题行
outfile = open('products.csv','w', newline='')
writer = csv.writer(outfile)
writer.writerow(["name", "offers", "price", "priceCurrency", "availability" ])
outfile.close()
alpha = soup.find_all('script',{'type':'application/ld+json'})
jsonObj = json.loads(alpha[1].text)
for item in jsonObj['itemListElement']:
name = item['name']
price = item['offers']['price']
currency = item['offers']['priceCurrency']
availability = item['offers']['availability'].split('/')[-1]
availability = [s for s in re.split("([A-Z][^A-Z]*)", availability) if s]
availability = ' '.join(availability)
解决方案
你没有得到结果,因为没有在循环中写入 CSV
outfile = open('products.csv','w', newline='')
writer = csv.writer(outfile)
writer.writerow(["name", "type", "price", "priceCurrency", "availability" ])
alpha = soup.find_all('script',{'type':'application/ld+json'})
jsonObj = json.loads(alpha[1].text)
for item in jsonObj['itemListElement']:
name = item['name']
type = item['@type']
price = item['offers']['price']
currency = item['offers']['priceCurrency']
availability = item['offers']['availability'].split('/')[-1]
# forgot this?
writer.writerow([name, type, price, currency, availability ])
# and close the CSV here
outfile.close()
推荐阅读
- java - 单击通知时如何将活动显示为弹出意图?
- c++ - 具有类型限定符的对象与成员函数不兼容
- discord.js - 如何将缩略图添加到 Discord 嵌入?
- time-complexity - 大哦!在 O(4^N) 中运行的算法
- visual-studio-2019 - .NET Core 3.1 在 VS2019 v16.6 中作为目标框架不可用
- java - OAuth 2.0 客户端 ID 和秘密 Java 实现
- python - 是否可以使用 python 将字段焦点设置为模糊或焦点在可填写的 PDF 文件表单字段中?
- augmented-reality - 为什么 ARCore + Sceneform 中的增强图像跟踪会出现故障,以及如何解决?
- ruby - 如何修改嵌套的哈希数组
- c# - 已解决:Linux 上的 ASP.NET CORE 和 EPPLUS 库 - IndexOutOfRangeException:工作表位置超出范围