Accessing Bing Webmaster API using Python to submit Urls

A few days ago, I used the requests library to post but it reported an error: format problem; today I found that we can use json=datato avoid it… But after that, I still don’t know why…

The code:

gist link:https://gist.github.com/cjh0613/28427e98e567cc28b31354fe92ce1c6e

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import requests

def get_(data):
headers={'User-Agent':'curl/7.12.1 ',
'Content-Type':'application/json'}
try:
r = requests.post(url='https://ssl.bing.com/webmaster/api.svc/json/SubmitUrl?apikey=API_KEY',json=data)
print(r.status_code)
print(r.content)
except Exception.e:
print(e)
#Example:
cjhpush={
"siteUrl": "https://cjh0613.github.io",
"url": "https://cjh0613.github.io/index.html"
}
print(cjhpush)
get_(cjhpush)

Get urls from sitemap:

This code is for google format sitemap (using ISO time eg 2020-05-28T10:54:43.663Z).Support hexo,etc.

Push a webpage link within 600 seconds of the sitemap.

gist link:https://gist.github.com/cjh0613/fb0daecee8b70de88c28802a3e72ede9

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
53
54
55
56
57
58
59
60
61
62
63
64
65
import requests
import json
import time
import datetime
import dateutil.parser
from bs4 import BeautifulSoup as bp

def get_(data):
headers={'User-Agent':'curl/7.12.1 ',
'Content-Type':'application/json'}
try:
r = requests.post(url='https://ssl.bing.com/webmaster/api.svc/json/SubmitUrl?apikey=APIKEY',json=data)
print(r.status_code)
print(r.content)
except Exception.e:
print(e)

print('start....','utf-8')
time.sleep(0.5)

site_url = 'https://cjh0613.github.io/blog/google-sitemap.xml'

try:
print('Get sitemap....','utf-8')
data_ = bp(requests.get(site_url).content,'lxml')
except Exception.e:
print(e)

list_url=[]
list_date=[]

print('---------------------------------')
#for x1,y1 in enumerate(data_.find_all('url')):
for x,y in enumerate(data_.find_all('loc')):
print(x,y.string)
list_url.append(y.string)

for x2,y2 in enumerate(data_.find_all('lastmod')):
startTime=y2.string
startTime=dateutil.parser.parse(startTime)
date1=(startTime.isoformat())[0:10]
startTime=date1+" "+(startTime.isoformat())[11:19]
startTime=datetime.datetime.strptime(startTime,"%Y-%m-%d %H:%M:%S")
now=datetime.datetime.utcnow()
endTime = datetime.datetime(now.year, now.month, now.day, now.hour, now.minute, now.second)
date2=(endTime.isoformat())[0:10]
date = endTime- startTime
seconds=date.seconds
if date1==date2 and seconds<600:#Can be modified
list_date.append(x2)

print('---------------------------------')
print(list_date)
print('submit....','utf-8')

for x in list_date:
cjhurl=list_url[x]
print('now:','utf-8' + cjhurl)

cjhpush={
"siteUrl": "web",#Need modifing
"url": cjhurl
}
print(cjhpush)
get_(cjhpush)

The URL Submission can be found here after logging in the Bing webmaster platform:

1
https://www.bing.com/webmasters/submiturl?siteUrl=[YourWebSite]

(You can also visitarchiveto look for posts)