一、API 介绍
官方API 常用方法
----------------------------------------浮点型
random.random():
Return the next random floating-point number in the range
0.0 <= X < 1.0
返回下一个随机【
浮点数】,范围在
[0, 1)之间,包含0,不包含1。
random.uniform(a , b):Return a random floating-point number N such that
a <= N <= b
fora <= b
andb <= N <= a
forb < a
返回一个随机
【
浮点数】,范围在
[a,b]之间,且忽略a、b大小的先后顺序。
如果想返回一个包含右边界的随机数,且忽略a、b大小的先后顺序,可以使用该方法。
----------------------------------------整型
random.randrange(stop):
random.randrange(start , stop [, step]):
Return a randomly selected element from
range(start, stop, step)
从 range(start, stop, step) 返回一个随机选择的元素。
- 该函数返回【整数】
- 范围在 **[start, stop)**之间,不包含 stop
- start 缺失的话,范围在 [0, stop)之间
- step 步长,常用来控制 奇偶
random.randint(a , b):Return a random integer N such that
a <= N <= b
. Alias forrandrange(a, b+1)
返回一个随机【整数 】,范围在 [a, b] 之间, 相当于
randrange(a, b+1)
random.choice(seq):Return a random element from the non-empty sequence seq . If seq is empty, raises IndexError
从非空序列 seq 返回一个随机元素。 如果 seq 为空,则引发 IndexError。
二、代码示例
1、random.random()
python
import random
# 返回下一个随机【浮点数】,范围在[0, 1)之间,包含0,不包含1
print( random.random() )
print( random.random() )
print( random.random() )
print( random.random() )
print( random.random() )
print( random.random() )
print( random.random() )
print( random.random() )
2、random.uniform(a , b)
python
import random
# a < b
print( random.uniform(1, 2) )
print( random.uniform(1, 2) )
# a > b
print( random.uniform(2, 1) )
print( random.uniform(2, 1) )
3、random.randrange(stop)
python
import random
# random.randrange(stop) 不包含 stop
# Integer from 0 to 1 inclusive
print( random.randrange(2) )
print( random.randrange(2) )
print( random.randrange(2) )
print( random.randrange(2) )
print( random.randrange(2) )
print( random.randrange(2) )
4、random.randrange(start , stop [, step])
python
import random
# 输出 0-10 随机 偶数
print( random.randrange(0, 11, 2) )
print( random.randrange(0, 11, 2) )
print( random.randrange(0, 11, 2) )
print( random.randrange(0, 11, 2) )
print( random.randrange(0, 11, 2) )
print( random.randrange(0, 11, 2) )
print('-------------------------------')
# 输出 0-10 随机 奇数
print( random.randrange(1, 11, 2) )
print( random.randrange(1, 11, 2) )
print( random.randrange(1, 11, 2) )
print( random.randrange(1, 11, 2) )
print( random.randrange(1, 11, 2) )
print( random.randrange(1, 11, 2) )
5、random.randint(a , b)
python
import random
# [0, 1]
print( random.randint(0, 1) )
print( random.randint(0, 1) )
print( random.randint(0, 1) )
print( random.randint(0, 1) )
print( random.randint(0, 1) )
print( random.randint(0, 1) )
6、 random.choice(seq)
python
import random
temp_list = [1, 2, 3]
print( random.choice( temp_list ) )
print( random.choice( temp_list ) )
print( random.choice( temp_list ) )
print( random.choice( temp_list ) )
print( random.choice( temp_list ) )
print( random.choice( temp_list ) )
print( random.choice( temp_list ) )
三、实战案例
1、随机时间请求
我们在用爬虫爬数据的时候,经常需要休眠一定时间,要么等待请求响应结束,要么等待网页加载结束。
python
import time
import requests
time.sleep(2)
response = requests.get(url)
但是这种固定时间间隔的请求,很容易被认定为是爬虫。
所以需要设置随机时间。
python
import random
import requests
# 休眠 1-2 秒的随机时间
time.sleep( random.uniform(1, 2) )
response = requests.get(url)
2、User-Agent 伪装
我们用 python requests 库直接请求的话,User-Agent 会被设置为 Python,这样请求很容易被服务器直接拦掉。所以需要对请求头进行伪装。
python
import random
import requests
agent_list = [
"Mozilla/5.0 (Linux; U; Android 2.3.6; en-us; Nexus S Build/GRK39F) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
"Avant Browser/1.2.789rel1 (http://www.avantbrowser.com)",
"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/532.5 (KHTML, like Gecko) Chrome/4.0.249.0 Safari/532.5",
"Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US) AppleWebKit/532.9 (KHTML, like Gecko) Chrome/5.0.310.0 Safari/532.9",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.514.0 Safari/534.7",
"Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/534.14 (KHTML, like Gecko) Chrome/9.0.601.0 Safari/534.14",
"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.14 (KHTML, like Gecko) Chrome/10.0.601.0 Safari/534.14",
"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.20 (KHTML, like Gecko) Chrome/11.0.672.2 Safari/534.20",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.27 (KHTML, like Gecko) Chrome/12.0.712.0 Safari/534.27",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.24 Safari/535.1",
"Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.36 Safari/535.7",
"Mozilla/5.0 (Windows; U; Windows NT 6.0 x64; en-US; rv:1.9pre) Gecko/2008072421 Minefield/3.0.2pre",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10"
]
# 设置请求头
headers = {
'User-Agent': random.choice(agent_list),
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
# 可以根据需要添加更多的请求头字段
}
time.sleep( random.uniform(1,2) )
response = requests.get(url, headers=headers)