赞
踩
首先加载库,找到包含数据的网址,拿到页码源代码
- import json2
- import requests
- from bs4 import BeautifulSoup
- import csv
- import os
-
- url = "http://www.xinfadi.com.cn/getCat.html"
- resp=requests.get(url)
- resp.encoding = "utf-8" # 修改源代码编码格式
使用json2解析数据,并保存
- print(resp.text)
- page = json2.loads(resp.text)
- path=os.path.exists("D:/case/菜价")
- if not path:
- os.makedirs("D:/case/菜价")
- f=open("D:/case/菜价/data.csv",'a')
- csvwrite=csv.writer(f)
- for row in page["list"]:
- name=row["prodName"]
- cat = row["prodCat"]
- lowPrice = row["lowPrice"]
- highPrice = row["highPrice"]
- avgPrice = row["avgPrice"]
- place = row["place"]
- csvwrite.writerow([name,cat,lowPrice,highPrice,avgPrice,place])
- f.close()
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。