Python 抓取數據存儲到Redis中

  redis是一個key-value存儲結構。和Memcached相似,它支持存儲的value類型相對更多,包括string(字符串)、list(鏈表)、set(集合)、zset(sorted set 有序集合)和hash(哈希類型),數據存儲以下圖分析html

  

  

  爲了分別爲ID存入多個鍵值對,這次僅對Hash數據進行操做,例子以下redis

import os,sys
import requests
import bs4
import redis

#鏈接Redis
r = redis.Redis(host='127.0.0.1',password='123456',port=6379)

html = 'https://www.dongmanmanhua.cn/dailySchedule?weekday=MONDAY'
result = requests.get(html)
texts = result.text

data = bs4.BeautifulSoup(texts,'html.parser');
lidata = data.select('div#dailyList ul.daily_card li')
#print(lidata)

for x in lidata:
    did = x.get('data-title-no')
    name = x.select('p.subj')
    name1 = name[0].get_text()
    url = x.a.get('href')
    story = x.a.p
    story1 = story.string
    user = x.select('p.author')
    user1 = user[0].get_text()
    like = x.select('em.grade_num')
    like1 = like[0].get_text()

    rt = {'did':did,'name':name1,'url':url,'story':story1,'user':user1,'like':like1}

    #寫數據到Redis
    idkey = 'name'+did
    #hash表數據寫入命令hmget,能夠一次寫入多個鍵值對
    r.hmget(idkey,rt)
        
    #寫入命令hset,一次只能寫入一個鍵值對
    r.hset(idkey,'did',did)
    r.hset(idkey,'name',name1)
    r.hset(idkey,'story',story1)
    r.hset(idkey,'url',url)
    r.hset(idkey,'user',user1)
    r.hset(idkey,'like',like1)
print('dman哈希表寫入成功') print(r.hget(idkey,'did')) print(r.hget(idkey,'name'))

   

  Hash 類其餘經常使用操做app

  hset(name,key,value) :name對應的hash中設置一個鍵值對,當name對應的hash中不存在當前key則建立(至關於添加) ,不然作更改操做
  hget(name,key) : 在name對應的hash中獲取根據key獲取value
  hmset(name,mapping) :在name對應的hash中批量設置鍵值對 ,mapping:例 {'k1':'v1','k2':'v2'}
  hmget(name,keys,*args) :在name對應的hash中獲取多個key的值 ,keys:要獲取key的集合,例 ['k1','k2'];*args:要獲取的key,如:k1,k2,k3 
  hgetall(name):獲取name對應hash的全部鍵值
  hlen(name):獲取name對應的hash中鍵值的個數
  hkeys(name):獲取name對應的hash中全部的key的值
  hvals(name):獲取name對應的hash中全部的value的值
  hexists(name,key):檢查name對應的hash是否存在當前傳入的key
  hdel(name,*keys):將name對應的hash中指定key的鍵值對刪除

url

相關文章
相關標籤/搜索