python之json模块及jsonpath

import json
import jsonpath
str_list='[1,2,3,4]'
str_dict='{"city":"绍兴"}'
list_=json.loads(str_list)           # loads时将json数据变成python数据
dict_=json.loads(str_dict)           # loads与load的区别是loads对字符串,load对文件
print(ls1)
print(dc1)
str_list=json.dumps(list_)                      # dumps时将python数据变成json数据
str_dict=json.dumps(dict_,ensure_ascii=False)  # 默认ascii解码,中文会乱码
print(str_list)
print(str_dict)
bookJson = '''
{ "store": {
      "book": [ 
        { "category": "reference",
          "author": "Nigel Rees",
          "title": "Sayings of the Century",
          "price": 8.95
        },
        { "category": "fiction",
          "author": "Evelyn Waugh",
         "title": "Sword of Honour",
         "price": 12.99,
         "isbn": "0-553-21311-3"
       }
     ],
     "bicycle": {
       "color": "red",
       "price": 19.95
     }
   }
 }
'''
books=json.loads(bookJson)
jsonpath.jsonpath(books,"$.store.bicycle.color")
jsonpath.jsonpath(books,'$.store.book[*]')
jsonpath.jsonpath(books,'$.store.book[0]')
jsonpath.jsonpath(books,'$.store.book[*].title')
jsonpath.jsonpath(books,'$.store.book[?(@.category=="fiction")]')\
jsonpath.jsonpath(books,'$.store.book[?(@.price<10)]')
jsonpath.jsonpath(books,'$.store.book[?(@.isbn)]')
import urllib
url="https://www.lagou.com/lbs/getAllCitySearchLabels.json"
headers={"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0"}
request=urllib.request.Request(url,headers=headers)
response=urllib.request.urlopen(request)
html=response.read().decode('utf-8')
print(html)
html_py=json.loads(html)
city_list=jsonpath.jsonpath(html_py,'$..name')
print(city_list)
file=open('city.json','w')
content=json.dumps(city_list,ensure_ascii=False)
file.write(content)
file.close()
print(json.load(open('city.json')))
from bs4 import BeautifulSoup as bs
headers={"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0"}
base_url='https://job.e0575.com/list.php?cIx=5&page='
for i in range(1,4):
    url=base_url+str(i)
    request=urllib.request.Request(url,headers=headers)
    response=urllib.request.urlopen(request)
    html=response.read().decode('utf-8')
    html=bs(html,'lxml')
    result1=html.select('li[class="bg1"]')
    result2=html.select('li[class="bg3"]')
    result1+=result2
ht='''<li class="bg1" onmouseout="this.className='bg1'" onmouseover="this.className='bg2'">
<a class="a1" href="https://job.e0575.com/show.php?nId=3078588" target="_blank" title="经验:经验不限  性别:性别不限  地区:安昌">
<span>平面设计师</span>
<img src="images/j.gif"/> <img src="images/m/icon_job_rz6.png" style="width:40px; height:16px;"/>
</a>
<div class="dd4">
<a href="http://www.e0575.com/user.php?nId=118576" target="_blank" title="绍兴招聘 绍兴柯桥绣禾装饰材料有限公司 招聘 平面设计师,待遇:3000-5000元">
						绍兴柯桥绣禾装饰材料有限公司						</a>
<span class="lv lv3"></span>
<img height="16" src="images/wx_pc.png" title="该企业已绑定了微信" width="16"/>
</div>
<div class="dd1">
						3000-5000元					</div>
<div class="dd2">
						今天 15:30					</div>
</li>'''
a=bs(ht,'lxml')
#a.find('span').text
#a.find('a').attrs['href']
#a.select('.dd2')[0].text[7:-5]
#a.select('.dd2')[0].text[7:-5]
#a.find('a',{'class':None}).text[7:-6]
a.find('a').attrs['title'].replace('\u3000','  ')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值