-
Notifications
You must be signed in to change notification settings - Fork 6
/
bing_image.py
45 lines (34 loc) · 1.12 KB
/
bing_image.py
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
# -*- coding: utf-8 -*-
"""
@File : bing_image.py
@Date : 2022-10-26
@Author : Peng Shiyu
"""
import json
import re
from datetime import datetime
from urllib.parse import urljoin
import requests
def get_bing_image():
url = 'https://cn.bing.com'
headers = {
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36'
}
res = requests.get(url, headers=headers)
res.encoding = res.apparent_encoding
ret = re.search("var _model =(\{.*?\});", res.text)
if not ret:
return
data = json.loads(ret.group(1))
image_content = data['MediaContents'][0]['ImageContent']
return {
'date': datetime.now().strftime('%Y-%m-%d'),
'headline': image_content['Headline'],
'title': image_content['Title'],
'description': image_content['Description'],
'image_url': urljoin(url, image_content['Image']['Url']),
'main_text': image_content['QuickFact']['MainText']
}
if __name__ == '__main__':
res = get_bing_image()
print(json.dumps(res, ensure_ascii=False, indent=2))