1

There are 10 outputs in json but it is only the first output is shown. How to show all 10 outputs?

from collections import OrderedDict
import requests
from lxml import html


@app.route('/saptop', methods=['GET'])
def saptop():

    page_indo = requests.get('http://www.waterfrontsekuritas.com/marketview')
    indo = html.fromstring(page_indo.content)
    indo = indo.xpath('//table[@id="top-gainer"]//td/text()')

    col = ['Stockcode','Lastprice','Prevprice','pc','Change','Tfreq','Vol','Value']

    c1 = [OrderedDict(zip(col,indo))]    
    return jsonify({'Stock': c1})

The current output only shown the first one

{
  "Stock": [
    {
      "Stockcode": "BOSS",
      "Lastprice": "1,400",
      "Prevprice": "1,120",
      "pc": "25.00",
      "Change": "280",
      "Tfreq": "3,640",
      "Vol": "23,087,100",
      "Value": "29,132,765,000"
    }
  ]
}

when change to c1 = [OrderedDict(zip(col,t)) for t in indo] , the output as below which is wrong also, all the string is seperated by letter and number:

{
  "Stock": [
    {
      "Stockcode": "B",
      "Lastprice": "O",
      "Prevprice": "S",
      "pc": "S"
    },
    {
      "Stockcode": "1",
      "Lastprice": ",",
      "Prevprice": "4",
      "pc": "0",
      "Change": "0"
    },
    {
      "Stockcode": "1",
      "Lastprice": ",",
      "Prevprice": "1",
      "pc": "2",
      "Change": "0"
    },
    {
      "Stockcode": "2",
      "Lastprice": "5",
      "Prevprice": ".",
      "pc": "0",
      "Change": "0"
    },

how to shown all 10 results correctly in Flask? It is json format

1 Answer 1

1

You need to convert the list format from

indo = ['abc','123','cde','efg','112','123'] to [['abc','123'],['cde','efg'],['112','123']]

for your example, change just add

indo = indo.xpath('//table[@id="top-gainer"]//td/text()')
indo = [indo[n:n+8] for n in range(0, len(indo), 8)]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.