爬虫中获取的无法直接操作的复合类型
平时有没有在爬取内容或取出来无法直接操作的情况。
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f59a536240ff47eacbf8299cfeb1adf0.png#pic_center)
TypeError: string indices must be integers
试着来读取内容比较繁杂,而且有一些混合类型不好操作。
试试eval() 方法
eval(expression, globals=None, locals=None)
expression:该参数是一个字符串,python会使用globals字典和locals字典作为全局和局部的命名空间,将expression当做一个python表达式进行解析和计算。
globals:该参数管控的是一个全局的命名空间,也就是我们在计算表达式的时候可以使用全局的命名空间中的函数,如果这个参数被提供了,并且没有提供自定义的__builtins__,那么会将当前环境中的__builtins__拷贝到自己提供的globals里,然后才会进行计算。关于__builtins__,它是python的内建模块,也就是python自带的模块,不需要我们import就可以使用的,例如我们平时使用的int、str、abs等都在这个模块中。关于它的说明可以参照这篇文章:点击打开链接。如果globals没有被提供,则使用python的全局命名空间。
locals:该参数管控的是一个局部的命名空间,和globals类似,不过当它和globals中有重复的部分时,locals里的定义会覆盖掉globals中的,也就是当globals和locals中有冲突的部分时,locals说了算,它有决定权,以它的为准。如果locals没有被 提供的话,则默认为globals。
a,b,c = 1,2,3
print('a+b+c')
a+b+c
明显我们获取的内容只是单纯的字符串格式。
但是我想读取字符串内容,并且能执行其中代码含义
a,b,c = 1,2,3
print(eval('a+b+c'))
6
那么来个实际案例。
在和风天气获取相应天气数据:
import requests
import pprint
value = 'location=beijing&key=0e2193f91303463f8cfece1a2d6ece36'
url = 'https://geoapi.heweather.net/v2/city/lookup?%s'%value
response = requests.get(url)
lc = response.text
print(lc)
{"status":"200","location":[{"name":"北京","id":"101010100","lat":"39.90498","lon":"116.40528","adm2":"北京","adm1":"北京","country":"中国","tz":"Asia/Shanghai","utcOffset":"+08:00","isDst":"0","type":"city","rank":"10","fxLink":"http://hfx.link/2ax1"},{"name":"海淀","id":"101010200","lat":"39.95607","lon":"116.31031","adm2":"北京","adm1":"北京","country":"中国","tz":"Asia/Shanghai","utcOffset":"+08:00","isDst":"0","type":"city","rank":"15","fxLink":"http://hfx.link/2ay1"},{"name":"朝阳","id":"101010300","lat":"39.92148","lon":"116.48641","adm2":"北京","adm1":"北京","country":"中国","tz":"Asia/Shanghai","utcOffset":"+08:00","isDst":"0","type":"city","rank":"15","fxLink":"http://hfx.link/2az1"},{"name":"昌平","id":"101010700","lat":"40.21808","lon":"116.23590","adm2":"北京","adm1":"北京","country":"中国","tz":"Asia/Shanghai","utcOffset":"+08:00","isDst":"0","type":"city","rank":"23","fxLink":"http://hfx.link/2b31"},{"name":"房山","id":"101011200","lat":"39.73553","lon":"116.13916","adm2":"北京","adm1":"北京","country":"中国","tz":"Asia/Shanghai","utcOffset":"+08:00","isDst":"0","type":"city","rank":"23","fxLink":"http://hfx.link/2b81"},{"name":"通州","id":"101010600","lat":"39.90248","lon":"116.65859","adm2":"北京","adm1":"北京","country":"中国","tz":"Asia/Shanghai","utcOffset":"+08:00","isDst":"0","type":"city","rank":"23","fxLink":"http://hfx.link/2b21"},{"name":"丰台","id":"101010900","lat":"39.86364","lon":"116.28696","adm2":"北京","adm1":"北京","country":"中国","tz":"Asia/Shanghai","utcOffset":"+08:00","isDst":"0","type":"city","rank":"25","fxLink":"http://hfx.link/2b51"},{"name":"大兴","id":"101011100","lat":"39.72890","lon":"116.33803","adm2":"北京","adm1":"北京","country":"中国","tz":"Asia/Shanghai","utcOffset":"+08:00","isDst":"0","type":"city","rank":"25","fxLink":"http://hfx.link/2b71"},{"name":"延庆","id":"101010800","lat":"40.46532","lon":"115.98500","adm2":"北京","adm1":"北京","country":"中国","tz":"Asia/Shanghai","utcOffset":"+08:00","isDst":"0","type":"city","rank":"33","fxLink":"http://hfx.link/2b41"},{"name":"平谷","id":"101011500","lat":"40.14478","lon":"117.11233","adm2":"北京","adm1":"北京","country":"中国","tz":"Asia/Shanghai","utcOffset":"+08:00","isDst":"0","type":"city","rank":"33","fxLink":"http://hfx.link/2bb1"}],"refer":{"sources":["heweather.com"],"license":["commercial license"]}}
查询其中的数据类型:
<class 'str'>
字符串格式,修改起来比较麻烦,可以直接转化成相应格式来操作
lc1 = eval(lc)
#print(type(lc1))
#print(type(lc))
for i in range(10):
print('地区',lc1['location'][i]['name'])
print('纬度',lc1['location'][i]['lat'])
print('经度',lc1['location'][i]['lon'])
地区 北京
纬度 39.90498
经度 116.40528
地区 海淀
纬度 39.95607
经度 116.31031
地区 朝阳
纬度 39.92148
经度 116.48641
地区 昌平
纬度 40.21808
经度 116.23590
地区 房山
纬度 39.73553
经度 116.13916
地区 通州
纬度 39.90248
经度 116.65859
地区 丰台
纬度 39.86364
经度 116.28696
地区 大兴
纬度 39.72890
经度 116.33803
地区 延庆
纬度 40.46532
经度 115.98500
地区 平谷
纬度 40.14478
经度 117.11233
OK!基本上在操作的时候,eval() 方法会比较方便解决我们的一些文本格式代码的问题。