好玩的API调用之---星座运势的API与爬虫爬取

algorain

好玩的API调用之—星座运势API与爬虫

平时写程序经常需要用到一些服务,像翻译,天气预报,星座什么的,我一般都是用Python写个爬虫去提供这些服务的网站爬数据,但是有些网站对爬虫有很多限制,一些关键字会定时更改,就像中国天气网经常变更HTML标签的class值,这就需要时常维护爬虫,而聚合数据API只对普通用户提供一个免费API接口,简直垃圾,而网上的一些网站其实有开放的API供开发者调用,所以我想着把自己发现的好玩的API和自己写的爬虫写个博客专题供大家参考,我会继续补充和维护。

第二个专题是关于星座运势查询的,平时无聊的时候会用到这个,平时放在网站里显示一下也是可以的,以前我是直接Python爬星座屋网站里的数据去分析的,后来发现聚合数据提供这个API,而且调用的方法也很简单,今天就把这两个方法写出来,有需要的可以自己参考着做。

1. 这是星座屋的网站

然后用Python写个爬虫直接爬就可以了,我直接放出代码,不做解释了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#-*- coding=utf8 -*-
import sys
import MySQLdb
reload(sys)
sys.setdefaultencoding( "utf-8" )
import urllib2
from bs4 import BeautifulSoup
import time
def download(url,headers):
try:
request = urllib2.Request(url,headers=headers)
html = urllib2.urlopen(request).read()
# html = urllib2.urlopen(url).read()
except urllib2.URLError as e:
print "error"
print e.code #可以打印出来错误代号如404。
print e.reason #可以捕获异常
html = None
return html
def save(html):
f = open('thefile.txt', 'w')
f.write(html)
f.close()
def read_file():
f = open('thefile.txt', 'r')
html = f.read()
f.close()
return html
def get_html(url):
User_agent = 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0'
headers = {'User_agent': User_agent}
html = download(url, headers)
save(html)
def constellation():
url = 'http://www.xzw.com/fortune/pisces/1.html'
get_html(url)
html = read_file()
soup = BeautifulSoup(html)
# print soup.find_all('dl')
html2 = soup.find('div', class_='c_cont')
html2 = str(html2)
soup = BeautifulSoup(html2)
text = '双鱼座明日运势:' + '\n'
text = text + '整体运势:' + soup.find_all('span')[0].string + '\n'
text = text + '事业学业:' + soup.find_all('span')[2].string + '\n'
text = text + '健康运势:' + soup.find_all('span')[4].string + '\n'
return text
# html = urllib2.urlopen(url)
# print html
if __name__=='__main__':
weather_text = weather()

2.先在聚合数据里注册,然后再提交身份证照片进行个人认证,很烦的,最后在免费数据里找到星座运势,就是这样的

然后点击申请数据,再进行一遍验证,不过很快就通过了,在控制台找的自己的key,我是使用Python来处理数据的,下面是代码,把key直接换成你的就可以,至于类型,根据需要切换today,tomorrow,week,year等。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#-*- coding=utf8 -*-
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
import urllib2
import json
from bs4 import BeautifulSoup
def download(url):
html = urllib2.urlopen(url)
return html.read()
#星座运势api的调取
def constellation():
today_url = 'http://web.juhe.cn:8080/constellation/getAll?consName=双鱼座&type=today&key='
tomorrow_url = 'http://web.juhe.cn:8080/constellation/getAll?consName=双鱼座&type=tomorrow&key='
html = download(tomorrow_url)
json_html = json.loads(html)
text = ''
text = text + str(json_html['name']) + ' ' + str(json_html['datetime']) + ' 明日运势 \n';
text = text + '综合指数:' + str(json_html['all']) + '\n'
text = text + '幸运色:' + str(json_html['color']) + '\n'
text = text + '健康指数:' + str(json_html['health']) + '\n'
text = text + '爱情指数:' + str(json_html['love']) + '\n'
text = text + '财运指数:' + str(json_html['money']) + '\n'
text = text + '幸运数字:' + str(json_html['number']) + '\n'
text = text + '明日概述:' + str(json_html['summary']) + '\n'
print text
return text
def main():
text = constellation()
if __name__ == "__main__":
main()
  • Title: 好玩的API调用之---星座运势的API与爬虫爬取
  • Author: algorain
  • Created at: 2017-04-25 20:59:52
  • Updated at: 2023-05-14 21:39:50
  • Link: http://www.rain1024.com/2017/04/25/api-article74/
  • License: This work is licensed under CC BY-NC-SA 4.0.
 Comments
On this page
好玩的API调用之---星座运势的API与爬虫爬取