Why url content cannot be print out?

 try:
      if self.ADULT.selected:
        dateStr = str(self.startDate.date)
        date = dateStr[0:10]
        u = self.Url(date,self.startStation.text,self.ReturnStation.text,'ADULT') # call Url function
        url = ur.urlopen(u) # open url
        urlContent = url.read() # read content of this url
        print(u)
        urlpartC = str(urlContent) # convert urlContent into String
        print("ooooo",urlpartC)
        print('done!')
    except:
      print('error!')

Ouput:
https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date=2019-03-23&leftTicketDTO.from_station=BJP&leftTicketDTO.to_station=SHH&purpose_codes=ADULT

                                 # why the url content is missing?

(‘ooooo’, ’ ') #convert to string doesn’t help also.
done!

Hi there,

Could you share a clone of the app that uses this code and describe in more detail what you are building? That might allow others to help you more easily.

You can clone an app by following these instructions:

As the image shown, it supposed to print out (u), (“ooooo”, urlcontent), and “done!”, now, u is here, ‘ooooo’ is here, urlcontent is missing, and “done!” is here. why was that?

I execute the same code in other python platform, like Spider, Jupyter Notebook, both of them are able to print out urlcontent.

Executed in Spyder.

Printing the out is not my goal, my goal is extracting those data needed ,then display them into another form. If any tutorial about this, please let me know.

Could you try moving the urllib calls to a server function? Here is an example of an app that prints the contents from a URL using the python3 runtime:

https://anvil.works/build#clone:OH3FWIFCBA2C5RZE=EXFYYV4MNID4XLINDVAYFWMR

I hope this helps. I’m sure others can be more helpful.

Thank you! It is very helpful! Now I got it.

1 Like