首页 > 技术文章 > python中正则随机值获取

boosli 2021-02-27 22:11 原文

#调用模块
import requests
import re
import random

response = requests.get(url='https://www.baidu.com/')#请求接口
body=response.content.decode('utf-8')#返回内容
value = re.findall(r'class=mnav>(.+?)</a>',body)#首页正则获取
print(value[random.randint(0,len(value)-1)])#随机输出

推荐阅读