1. requests的基本用法
import requests
作用1:爬网页数据
response = requests. get( 'https://cd.zu.ke.com/zufang' )
response. encoding = 'utf-8'
result = response. text
print ( result)
作用2:请求接口数据
response = requests. get( 'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js' )
result = response. json( )
for x in result[ 'hero' ] :
print ( x[ 'name' ] , x[ 'goldPrice' ] )
作用3:下载图片、视频、音频等
response = requests. get( 'https://img1.doubanio.com/view/photo/l/public/p2557573348.webp' )
result = response. content
with open ( 'files/a.png' , 'wb' ) as f:
f. write( result)
补充:json 数据
"""
要求:有且只有一个数据,并且这个数据是json支持的类型的数据。
json支持的数据类型的格式:
1)数字 - 和数学一样
2)字符串 - 只能使用双引号
3)布尔值 - true、false
4)空值 - null
5)数组(列表) - [数据1, 数据2, 数据3]
6)字典 - {键1:值1,键2:值2,键3:值3,... }(字典的键只能是字符串,值可以是任何类型的数据)
"""
import json
result = json. loads( '"abc"' )
print ( result, type ( result) )
result = json. loads( '100' )
print ( result, type ( result) )
result = json. loads( 'null' )
print ( result, type ( result) )
result = json. loads( '[10, "abc", true, null]' )
print ( result, type ( result) )
json. dumps( 100 )
json. dumps( 'abc' )
json. dumps( True )
result = json. dumps( { 'name' : '张三' , 'age' : 18 , '已婚' : False , 10 : 20 } )
print ( result)
2.下载英雄选择音频
import requests
def get_all_audio_url ( ) :
response = requests. get( 'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js' )
result = response. json( )
urls = [ ]
for x in result[ 'hero' ] :
url = x[ 'selectAudio' ]
name = x[ 'name' ]
urls. append( ( url, name) )
return urls
def download_audio ( hero) :
url = hero[ 0 ]
name = hero[ 1 ]
result = requests. get( url) . content
with open ( f'files/ { name} .ogg' , 'wb' ) as f:
f. write( result)
print ( f' { name} 音频下载完成' )
if __name__ == '__mian__' :
for x in get_all_audio_url( ) :
download_audio( x)
response = requests. get( 'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js' )
result = response. json( )
urls = [ ]
for x in result[ 'hero' ] :
result = requests. get( x[ 'selectAudio' ] ) . content
file = 'files' + x[ 'name' ] + '.ogg'
with open ( file , 'wb' ) as f:
f. write( result)
3.浏览器反爬
import requests
from re import findall
headers = {
'User-Agent' : 'xxxxxxxx'
}
response = requests. get( 'https://movie.douban.com/top250' , headers= headers)
result = response. text
print ( result)
names = findall( r'<img width="100" alt="(\w+)"' , result)
print ( names)
scores = findall( r'<span class="rating_num" property="v:average">(\d+\.?\d*)</span>' , result)
print ( scores)
result = map ( lambda i1, i2: { 'title' : i1, 'score' : i2} , names, scores)
print ( list ( result) )