前言 Python连接数据库是十分必要的操作,我这次用的是MongoDB数据库保存数据。这类的文章一般都不容易审核,所以就不多说废话,直接开搞,完整源码放在最后,有需要自取。
效果展示: 导入所需要的相关库
import requests
import pymongo
连接数据库并建库和集合 名称自取
client = pymongo.MongoClient(host='127.0.0.1',port=27017)
db = client.shucai
collection = db.shucai
定义函数,为这个集合传数据,一个一个的传
def save_mongo(dic):
collection.insert_one(dic)
定义函数main,用来获取信息以及传参的操作。
def main():
以下都是函数命令 表单数据
data = {
'limit':'',
'current':'',
'pubDateStartTime':'',
'pubDateEndTime':'',
'prodPcatid':'',
'prodCatid':'',
'prodName':'',
}
伪装头部
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/.058.102 Safari/537.36'
}
信息来源URL
url = 'http://www.xinfadi.com.cn/getPriceData.html'
外层定义一个range(取前不取后)用来规定获取的页数
for i in range(1,11,):
data = {'current':f'{i}'}
respomse = requests.post(url=url, data=data, headers=headers)
d = respomse.json()
li_list = d['list']
print(li_list)
对响应进行字典进行遍历,利用键精确获取相应的值
for li in li_list:
prodCat = li['prodCat']
prodPcat = li['prodPcat']
prodName = li['prodName']
lowPrice = li['lowPrice']
avgPrice = li['avgPrice']
highPrice = li['highPrice']
place = li['place']
specInfo = li['specInfo']
unitInfo = li['unitInfo']
pubDate = li['pubDate']
print(prodCat,prodPcat,prodName,lowPrice,avgPrice,highPrice,place,specInfo,unitInfo,pubDate)
为获取到的信息取别称
dic = {
'一级分类':prodCat,
'二级分类':prodPcat,
'菜名':prodName,
'最低价':lowPrice,
'平均价':avgPrice,
'最高价':highPrice,
'产地':place,
'规格':specInfo,
'单位':unitInfo,
'日期':pubDate,
}
将参数传入到MongoDB数据库集合内
save_mongo(dic)
运行主函数
main()
完整代码:
import requests
import pymongo
client = pymongo.MongoClient(host='127.0.0.1',port=27017)
db = client.shucai
collection = db.shucai
def save_mongo(dic):
collection.insert_one(dic)
def main():
data = {
'limit':'',
'current':'',
'pubDateStartTime':'',
'pubDateEndTime':'',
'prodPcatid':'',
'prodCatid':'',
'prodName':'',
}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/9
}
url = 'http://www.xinfadi.com.cn/getPriceData.html'
for i in range(1,11,):
data = {'current':f'{i}'}
respomse = requests.post(url=url, data=data, headers=headers)
d = respomse.json()
li_list = d['list']
print(li_list)
for li in li_list:
prodCat = li['prodCat']
prodPcat = li['prodPcat']
prodName = li['prodName']
lowPrice = li['lowPrice']
avgPrice = li['avgPrice']
highPrice = li['highPrice']
place = li['place']
specInfo = li['specInfo']
unitInfo = li['unitInfo']
pubDate = li['pubDate']
print(prodCat,prodPcat,prodName,lowPrice,avgPrice,highPrice,place,specInfo,unitInfo,pubDate)
dic = {
'一级分类':prodCat,
'二级分类':prodPcat,
'菜名':prodName,
'最低价':lowPrice,
'平均价':avgPrice,
'最高价':highPrice,
'产地':place,
'规格':specInfo,
'单位':unitInfo,
'日期':pubDate,
}
save_mongo(dic)
main()
|