I am getting some strange Index error ONLY some of the time with the following codes:
# coding=utf-8
from bs4 import BeautifulSoup as bs
from urllib import urlopen
import json
import csv
link = 'https://familysearch.org/pal:/MM9.1.1/KH21-F11'
Soup = bs(urlopen(link).read())
# Process into Json, plus index error control
rawJ = Soup.find_all('script')
J = str(rawJ[10])
J1 = J.split('var person = ')
J2 = J1[1].rsplit('var record =')
J3 = J2[0].rsplit(';', 1)
JsonText = J3[0].decode('utf-8')
s = json.loads(JsonText)
# Declare
name = s["personBestName"]
About 2-4 out of 10 times (randomly) running this same script on the same link, I get the error as follow:
Traceback (most recent call last):
File "C:\Users\User\Desktop\_test8.py", line 16, in <module>
J2 = J1[1].rsplit('var record =')
IndexError: list index out of range
[Finished in 2.8s with exit code 1]