python +flask 制作一个实时热搜采集接口 加入百度热搜和微博热搜 并实时滚动代码 嵌入到你的帝国CMS或WP博客 或任意框架网站

**示例帝国CMS网站效果:**
![在这里插入图片描述](https://img-blog.csdnimg.cn/b3dc23ac54c048d6b022cda34f786fb1.gif?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1poaU1hb1lpRGVIdWFpUmVu,size_16,color_FFFFFF,t_70#pic_center)超级简单的实现,两个步骤搞定:
1.写python采集接口,采集数据。
接口代码:需要安装对应的库。
```python
# coding=utf-8
import re
import urllib.parse
import random
import requests
import datetime
from flask import Flask
from flask import request

def getweibo():
url = 'https://s.weibo.com/top/summary?cate=realtimehot'
h = {
'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0",
}
response = requests.get(url, headers=h).content.decode('utf-8')
# print("网页内容",response)
pat = '<td class="td-02">[\S\s]*?<a href="(.*?)" target="_blank">(.*?)</a>'
keyword = re.findall(pat, response, re.S)
txt=htxt=""
a=0
print(len(keyword))
for key in keyword:
link="https://s.weibo.com"+key[0]
hot_word=key[1]
randtxt = random.randint(5, 20) * "&nbsp;"
txt = txt + "{}<a href='{}' target='_blank' >{}</a>".format(randtxt, link, hot_word)
a = a + 1
if a % 20 == 0 or len(keyword)==a:
txt = "<MARQUEE scrollAmount=2>" + txt + "</MARQUEE><br><br>"
htxt = htxt + txt
txt = ""
return htxt

def getbaidu():
url = 'https://www.baidu.com/'
h = {
'Host': 'www.baidu.com',
'Referer': 'https://www.baidu.com/',
'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0",
}
response = requests.get(url, headers=h).content.decode('utf-8')
# 获取关键字
pat = '"pure_title": "(.*?)","linkurl": "(.*?)"'
keyword = re.findall(pat, response, re.S)
arr = {""}
txt=htxt=randtxt=""
a = 0
for hot_word in keyword:
#i = urllib.parse.quote(hot_word, encoding='utf-8', errors='replace')# 汉字不符合url标准,所以这里需要进行url编码
link = urllib.parse.unquote(hot_word[1])

txt=txt+"{}<a href='{}' target='_blank' >{}</a>".format(random.randint(5,20)*"&nbsp;",link,hot_word[0])
a = a + 1
if a % 15 == 0 or len(keyword)==a:
txt = "<MARQUEE scrollAmount=4>" + txt + "</MARQUEE>"
htxt=htxt+txt
txt=""
htxt=htxt+getweibo()
if len(keyword)>5:
filename=r"txt/{}.txt".format(str(datetime.date.today()))
file=open(filename,"w+")
file.write(htxt)
file.close()

zd={
"#东京奥运直播#":"https://www.baidu.com/s?wd=%E5%A5%A5%E8%BF%90%E7%9B%B4%E6%92%AD",
"#英雄联盟 夏季赛直播#":"https://live.bilibili.com/6?hotRank=0&session_id=60ae34169f73e9d5_A445FBA6-BC74-4910-A40B-4ECDC175387D&visit_id=9b9lgno8cgg0",
"新奇探索#老高与小茉#":"https://www.bilibili.com/video/BV1xv411L7Fw?from=search&seid=3288198967905026996"

}
zdtxt=""#定义自定义的链接,置顶,其实是靠后的
for title,link in zd.items():
zdtxt=zdtxt+"{}<a href='{}' target='_blank' >{}</a>".format(random.randint(5,20)*"&nbsp;",link,title)
zdtxt = "<MARQUEE scrollAmount=4>" + zdtxt + "</MARQUEE>"
return htxt+zdtxt

app = Flask(__name__)
@app.route("/newhot/", methods=['GET', 'POST'])
def wx_falsk():
if request.method == "GET":
mode = request.args.get("mode", '0')
if request.method == "POST":
mode = request.values.get("mode", "0")
if mode=="1":
txt=getbaidu()
else:
try:
filename=r"txt/{}.txt".format(str(datetime.date.today()))
file = open(filename, "r")
txt = file.read()
file.close()
except:
txt = getbaidu()
print("xt",txt)
return txt

if __name__ == '__main__':
app.run(host='0.0.0.0', port="9529", debug=False)
#app.run(host='127.0.0.1', port="9529", debug=True)

```

2.到网站调用这个接口。
PHP调用代码:

```php
<p style="font-size:26px;color:#FFFFFF" ><?php
echo file_get_contents("http://xxx.xxx.xxx.xxx:9529/newhot/");
?> </p>
```
其中http://xxx.xxx.xxx.xxx:9529/newhot/是python接口地址,

再次将这个代码放到wp博客调用:

![在这里插入图片描述](https://img-blog.csdnimg.cn/85e98e8c585f4a81ac1199d8f8dbd84f.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1poaU1hb1lpRGVIdWFpUmVu,size_16,color_FFFFFF,t_70)

## **怎么样,是不是超级简单,今天你又学废了吗?

有疑问联系微信:huang582716403 注明来意,不要反复问基础问题。**

评论0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
登入/注册
卧槽~你还有脸回来
没有账号? 忘记密码?
');})();