I have tried the aboveHere is the code to scrape Bursa Malaysia stock exchange. Bursa Malaysia has about 968 stocks but the code below only scrape 40 of it. Any amendment to the code?
import requests import json import time
BASE_URL = "https://in.all symbols present in that yahoo-finance.yahoo screener.com/screener/unsaved/4a033d87-0423-46a6-9b83-5565629ef366?dependentField=sector&dependentValues="
cnt = 100 offset = 0 flag = 1 temp = 10# You can simply create a screener https://in.finance.yahoo.com/screener/unsaved/4a033d87-0423-46a6-9b83-5565629ef366?dependentField=sector&dependentValues=
import requests import json import timehere
BASE_URL = "https://in.I have modified the code a little bit because yahoo-finance.yahoo was returning an empty 404 response when working with the python requests module.com/screener/unsaved/4a033d87-0423-46a6-9b83-5565629ef366?dependentField=sector&dependentValues="
cnt = 100 offset = 0 flag = 1 temp = 10 name_to_symbol = dict() Similar to this issue / solution
while flag > 0: url = BASE_URL.replace("OFFSET", str(offset)) url = urlHere is the complete working code.replace("COUNT",str(cnt)) offset += cnt
response = requests.get(url)
s = str(response.text)
jsonArrayStart = s.find('"results":{"rows"') + 18
jsonArrayEnd = jsonArrayStart + 1
while s[jsonArrayEnd] != ']':
jsonArrayEnd += 1
jsonArrayString = s[jsonArrayStart: jsonArrayEnd+1]
jsonArray = json.loads(jsonArrayString)
print(len(jsonArray))
if len(jsonArray) != cnt:
flag = 0
for obj in jsonArray:
if 'longName' in obj:
name_to_symbol[obj['symbol']] = obj['longName']
elif 'shortName' in obj:
name_to_symbol[obj['symbol']] = obj['shortName']
else:
name_to_symbol[obj['symbol']] = obj['symbol']
time.sleep(10)
name_to_symbol name_to_symbol = dict()
import requests
import json
import time
# Modify BASE_URL accordingly
BASE_URL = "https://finance.yahoo.com/screener/unsaved/79fce2f7-82fe-49ec-8098-88add84138ec?dependentField=sector&dependentValues=&offset=OFFSET&count=COUNT"
cnt = 100
offset = 0
flag = 1
temp = 10
name_to_symbol = []
total = 0
while flag > 0:
url = BASE_URL.replace("OFFSET", str(offset))
url = url.replace("COUNT",str(cnt))
offset += cnt
response = requests.get(url, headers={'User-Agent': 'Custom'})
s = str(response.text)
jsonArrayStart = s.find('"results":{"rows"') + 18
jsonArrayEnd = jsonArrayStart + 1
while s[jsonArrayEnd] != ']':
jsonArrayEnd += 1
jsonArrayString = s[jsonArrayStart: jsonArrayEnd+1]
jsonArray = json.loads(jsonArrayString)
if len(jsonArray) != cnt:
flag = 0
total += len(jsonArray)
for obj in jsonArray:
if 'longName' in obj:
name_to_symbol.append([obj['symbol'], obj['longName']])
elif 'shortName' in obj:
name_to_symbol.append([obj['symbol'], obj['shortName']])
else:
name_to_symbol.append([obj['symbol'], obj['symbol']])
time.sleep(10)
name_to_symbol
while flag > 0: url = BASE_URLP.replace("OFFSET", str(offset)) url = urlS - Sorry for the late response.replace("COUNT" Also,str(cnt)) offset += cnt I have rejected all the edits as they were irrelevant and I have fixed the code now. In future I would recommend commenting on the post instead of suggesting an improper edit
response = requests.get(url)
s = str(response.text)
jsonArrayStart = s.find('"results":{"rows"') + 18
jsonArrayEnd = jsonArrayStart + 1
while s[jsonArrayEnd] != ']':
jsonArrayEnd += 1
jsonArrayString = s[jsonArrayStart: jsonArrayEnd+1]
jsonArray = json.loads(jsonArrayString)
print(len(jsonArray))
if len(jsonArray) != cnt:
flag = 0
for obj in jsonArray:
if 'longName' in obj:
name_to_symbol[obj['symbol']] = obj['longName']
elif 'shortName' in obj:
name_to_symbol[obj['symbol']] = obj['shortName']
else:
name_to_symbol[obj['symbol']] = obj['symbol']
time.sleep(10)
name_to_symbolHTH