Skip to main content
Discarded suggested edits. Fixed the code.
Source Link

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

I have tried the above 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.finance.yahoo.com/screener/unsaved/4a033d87-0423-46a6-9b83-5565629ef366?dependentField=sector&dependentValues="

cnt = 100 offset = 0 flag = 1 temp = 10# https://in.finance.yahoo.com/screener/unsaved/4a033d87-0423-46a6-9b83-5565629ef366?dependentField=sector&dependentValues=

import requests import json import time

BASE_URL = "https://in.finance.yahoo.com/screener/unsaved/4a033d87-0423-46a6-9b83-5565629ef366?dependentField=sector&dependentValues="

cnt = 100 offset = 0 flag = 1 temp = 10 name_to_symbol = dict()

while flag > 0: url = BASE_URL.replace("OFFSET", str(offset)) url = url.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()

while flag > 0: url = BASE_URL.replace("OFFSET", str(offset)) url = url.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

Here is the code to scrape all symbols present in that yahoo-finance screener. You can simply create a screener here

I have modified the code a little bit because yahoo-finance was returning an empty 404 response when working with the python requests module. Similar to this issue / solution

Here is the complete working code.

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

P.S - Sorry for the late response. Also, 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

HTH

Rollback to Revision 2 - Edit approval overridden by post owner or moderator
Source Link

I have tried the above the code to scrape Bursa Malaysia stock exchangescrape Bursa Malaysia stock exchange. Bursa Malaysia has about 968 stocks but the code below only scrape 40 of it. Any amendment to the code?

Hope for helpimport requests import json import time

BASE_URL = "https://in. Thanks in advancefinance.yahoo.com/screener/unsaved/4a033d87-0423-46a6-9b83-5565629ef366?dependentField=sector&dependentValues="

cnt = 100 offset = 0 flag = 1 temp = 10# https://in.finance.yahoo.com/screener/unsaved/4a033d87-0423-46a6-9b83-5565629ef366?dependentField=sector&dependentValues=

import requests import json import time

BASE_URL = "https://in.finance.yahoo.com/screener/unsaved/4a033d87-0423-46a6-9b83-5565629ef366?dependentField=sector&dependentValues="

cnt = 100 offset = 0 flag = 1 temp = 10 name_to_symbol = dict()

while flag > 0: url = BASE_URL.replace("OFFSET", str(offset)) url = url.replace("COUNT",str(cnt)) offset += cnt

importresponse = requests.get(url)
imports json
import= time
str(response.text)
BASE_URLjsonArrayStart = "https://in.finance.yahoos.com/screener/unsaved/4a033d87-0423-46a6-9b83-5565629ef366?dependentField=sector&dependentValues="
find('"results":{"rows"') + 18
cntjsonArrayEnd = 100jsonArrayStart + 1
offset
while s[jsonArrayEnd] != 0']':
flag =   jsonArrayEnd += 1
temp 
jsonArrayString = 10s[jsonArrayStart: jsonArrayEnd+1]
name_to_symboljsonArray = dictjson.loads(jsonArrayString)
print(len(jsonArray))
whileif len(jsonArray) != cnt:
    flag >= 0
    
for obj in jsonArray:
    urlif ='longName' BASE_URL.replace("OFFSET",in str(offset))obj:
    url    name_to_symbol[obj['symbol']] = url.replace("COUNT",str(cnt))obj['longName']
    offsetelif +='shortName' cntin 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()

while flag > 0: url = BASE_URL.replace("OFFSET", str(offset)) url = url.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

I have tried the above 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?

Hope for help. Thanks in advance.

import requests
import json
import time

BASE_URL = "https://in.finance.yahoo.com/screener/unsaved/4a033d87-0423-46a6-9b83-5565629ef366?dependentField=sector&dependentValues="

cnt = 100
offset = 0
flag = 1
temp  = 10
name_to_symbol = dict()

while flag > 0:
    url = BASE_URL.replace("OFFSET", str(offset))
    url = url.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
```

I have tried the above 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.finance.yahoo.com/screener/unsaved/4a033d87-0423-46a6-9b83-5565629ef366?dependentField=sector&dependentValues="

cnt = 100 offset = 0 flag = 1 temp = 10# https://in.finance.yahoo.com/screener/unsaved/4a033d87-0423-46a6-9b83-5565629ef366?dependentField=sector&dependentValues=

import requests import json import time

BASE_URL = "https://in.finance.yahoo.com/screener/unsaved/4a033d87-0423-46a6-9b83-5565629ef366?dependentField=sector&dependentValues="

cnt = 100 offset = 0 flag = 1 temp = 10 name_to_symbol = dict()

while flag > 0: url = BASE_URL.replace("OFFSET", str(offset)) url = url.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()

while flag > 0: url = BASE_URL.replace("OFFSET", str(offset)) url = url.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

Yahoo finance scraping code shown seems not scrape all the tickers data but only a little of it
Source Link

I have tried the above the code to scrape Bursa Malaysia stock exchangescrape 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.finance.yahoo.com/screener/unsaved/4a033d87-0423-46a6-9b83-5565629ef366?dependentField=sector&dependentValues="

cnt = 100 offset = 0 flag = 1 temp = 10# https://in.finance.yahoo.com/screener/unsaved/4a033d87-0423-46a6-9b83-5565629ef366?dependentField=sector&dependentValues=

import requests import json import time

BASE_URL = "https://in.finance.yahoo.com/screener/unsaved/4a033d87-0423-46a6-9b83-5565629ef366?dependentField=sector&dependentValues="

cnt = 100 offset = 0 flag = 1 temp = 10 name_to_symbol = dict()

while flag > 0: url = BASE_URLHope for help.replace("OFFSET", str(offset)) url = url Thanks in advance.replace("COUNT",str(cnt)) offset += cnt

response =import requests.get(url)
simport =json
import str(response.text)time
jsonArrayStart
BASE_URL = s.find('"results""https:{"rows"') + 18//in.finance.yahoo.com/screener/unsaved/4a033d87-0423-46a6-9b83-5565629ef366?dependentField=sector&dependentValues="
jsonArrayEnd
cnt = jsonArrayStart + 1
100
while s[jsonArrayEnd]offset != ']':0
    jsonArrayEndflag +== 1
 
jsonArrayStringtemp = s[jsonArrayStart:= jsonArrayEnd+1]10
jsonArrayname_to_symbol = json.loads(jsonArrayString)
print(lendict(jsonArray))
if len(jsonArray) != cnt:
   while flag => 0
    
for obj in jsonArray:
    ifurl 'longName'= inBASE_URL.replace("OFFSET", obj:str(offset))
        name_to_symbol[obj['symbol']]url = obj['longName']
    elif 'shortName' in obj:url.replace("COUNT",str(cnt))
        name_to_symbol[obj['symbol']]offset =+= obj['shortName']cnt
    else:
        name_to_symbol[obj['symbol']] = obj['symbol']

time.sleep(10)

name_to_symbol name_to_symbol = dict()

while flag > 0: url = BASE_URL.replace("OFFSET", str(offset)) url = url.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

I have tried the above 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.finance.yahoo.com/screener/unsaved/4a033d87-0423-46a6-9b83-5565629ef366?dependentField=sector&dependentValues="

cnt = 100 offset = 0 flag = 1 temp = 10# https://in.finance.yahoo.com/screener/unsaved/4a033d87-0423-46a6-9b83-5565629ef366?dependentField=sector&dependentValues=

import requests import json import time

BASE_URL = "https://in.finance.yahoo.com/screener/unsaved/4a033d87-0423-46a6-9b83-5565629ef366?dependentField=sector&dependentValues="

cnt = 100 offset = 0 flag = 1 temp = 10 name_to_symbol = dict()

while flag > 0: url = BASE_URL.replace("OFFSET", str(offset)) url = url.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()

while flag > 0: url = BASE_URL.replace("OFFSET", str(offset)) url = url.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

I have tried the above 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?

Hope for help. Thanks in advance.

import requests
import json
import time

BASE_URL = "https://in.finance.yahoo.com/screener/unsaved/4a033d87-0423-46a6-9b83-5565629ef366?dependentField=sector&dependentValues="

cnt = 100
offset = 0
flag = 1
temp  = 10
name_to_symbol = dict()

while flag > 0:
    url = BASE_URL.replace("OFFSET", str(offset))
    url = url.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
```
Loading
Source Link
Loading