来让我用Python算算你今天的“星座运势”?
1. 项目背景
2. 十二星座简介
3. 开发思路
定义一个函数,输入你的出生日期,获取对应的星座; 定义一个字典,根据不同的星座,得到星座对应的英文,用于拼接 URL; 获取你的星座运势。
def get_constellation(month, date):
dates = (21, 20, 21, 21, 22, 22, 23, 24, 24, 24, 23, 22)
constellations = ("摩羯座", "水瓶座", "双鱼座", "白羊座",
"金牛座", "双子座", "巨蟹座", "狮子座",
"处女座", "天秤座", "天蝎座", "射手座", "摩羯座")
if date < dates[month-1]:
return constellations[month-1]
else:
return constellations[month]
constellation = get_constellation(7, 21)
print(f'根据你的出生日期,判断你属于"{constellation}"')
2)定义一个字典,根据不同的星座,得到星座对应的英文,用于拼接 URL
dict_ = {"水瓶座":"Aquarius",
"双鱼座":"Pisces",
"白羊座":"Aries",
"金牛座":"Taurus",
"双子座":"Gemini",
"巨蟹座":"Cancer",
"狮子座":"Leo",
"处女座":"Virgo",
"天秤座":"Libra",
"天蝎座":"Scorpio",
"射手座":"Sagittarius",
"摩羯座":"Capricorn"}
url = f"https://www.xzw.com/fortune/{dict_[constellation]}/"
url
import requests
response = requests.get(url, verify=False)
response.encoding = 'utf-8'
content = response.text
lis = re.findall('<em style=" width:(.*?)px;">',content)
comprehensive_fortune,love_fortune,career_fortune,wealth_fortune = [str(int(int(i)/16))+"星" for i in lis]
health_index = re.findall('健康指数:</label>(.*?)<',content,re.S)[0]
negotiation_Index = re.findall('商谈指数:</label>(.*?)<',content,re.S)[0]
lucky_color = re.findall('幸运颜色:</label>(.*?)<',content,re.S)[0]
lucky_num = re.findall('幸运数字:</label>(.*?)<',content,re.S)[0]
match_constellation = re.findall('速配星座:</label>(.*?)<',content,re.S)[0]
short_comment = re.findall('短评:</label>(.*?)<',content,re.S)[0]
https://pan.baidu.com/s/1d1PtnRoGl1kNgU9Th8Z9Iw
提取码: rcv5
如果文章对你有帮助,欢迎转发/点赞/收藏~
作者:黄伟呢
_往期文章推荐_
评论