Python Forum
Problem with retrieving and parsing from webpage.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with retrieving and parsing from webpage.
#1
Hi all.

I was wondering if someone could help me out here. I'm stuck Huh

There's this piece of code I have for an Adafruit FM transmitter, connected to a Raspberry Pi. This works as expected:
while True:
        radio.setRDSbuffer( "Text 1")
        sleep(6)
        radio.setRDSbuffer( "Text 2")
        sleep(6)
But what I would like, is to have the 'Now Playing' info inserted into there.
Let's assume I have this 'Now Playing'-info here, in just one line of plain text: http://example.com/ontheair
This is what I came up with:

At the top of the script:
import urllib2
And further down the script:
while True:
        radio.setRDSbuffer( "Text 1")
        sleep(6)
        radio.setRDSbuffer( "Text 2")
        sleep(6)
        sock = urllib2.urlopen("http://example.com/ontheair")
        nowplaying = sock.read()
        sock.close()
        radio.setRDSbuffer(nowplaying)
        sleep(15)
But this doesn't work. That is... It works until it reaches the part where I try and retrieve/parse something from a webpage. So the loop stops after 'Text 2'.

I am obviously doing something wrong in the retrieving/parsing of the external data.....

Any suggestions? Big Grin
Reply
#2
Oh, and it's running v2.7.

This is my first Python attempt... It's a correctly working while true loop, it's just that all seems to go wrong when I try to get some text from a webpage. (From my own web page, for the record...)

I'd really appreciate it if someone with more Python experience could shed some light on this Smile
Reply
#3
What happens if you try to put the same address into the web browser's address bar and hit Enter?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
(May-15-2018, 05:40 PM)wavic Wrote: What happens if you try to put the same address into the web browser's address bar and hit Enter?

Then I get a white web page with one line of (plain) text, showing the title of the currently playing track, as expected.
(I use that page as a source in other (PHP) scripts as well. No problems there.)

Thx.
Reply
#5
What is that close method? This is not the socket library.
Run the script without it and print nowplaying to see if there is something there.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
Actually I grabbed something that others said worked (the code for retrieving text from a webpage) and tried to integrate that into my working while true loop.

At this point I'm not sure whether the retrieve code itself is sound. I do not know how to do what you are suggesting. Can you be more specific?
Excuse my n00b-ness Undecided
Reply
#7
I suggest you use requests library.

A simple example to get a web page content:
import requests

data = requests.get('http://example.com/data').content
print data
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
Because of this I now learned about requests, and that you may have to install that seperately, as I have done now. Thanks.

When I include round brackets, as a separate test script this works! It displays the line of text.
import requests

data = requests.get('http://example.com/data').content
print (data)
Now to integrate it...
This will display any text I manually put in between the quotation marks in the script. So this will actually display Text 1:
radio.setRDSbuffer( "Text 1")
But in this case it should display the contents of the string called 'data' as well.
I found the below code doesn't do the trick... And using single or double quotation marks results in actually displaying the word data, instead of the contents of the string by the same name.
import requests
while True:
        radio.setRDSbuffer( "Text 1")
        sleep(6)
        radio.setRDSbuffer( "Text 2")
        sleep(6)
        data = requests.get('http://example.com/data').content
        radio.setRDSbuffer(data)
        sleep(15)
Any and all tips are welcome :)
Reply
#9
What does radio.setRDSbuffer do?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
Any text placed in there is displayed as the RDS text you get on a regular FM radio.

If I was broadcasting on 100.7MHz, and did this:
radio.setRDSbuffer( "WMMS Cleveland's Rock Station")
This is what that would look like on an actual radio:
[Image: WMMS_Cleveland_RDS_display.JPG]

So I can put any kind of text there. As long as I enter it manually into the script.

That's why I'm trying to figure out how to get text in there from a webpage, so it can display in real time what actually is playing on the radio at that very moment :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  KeyError while retrieving ESPN data john317ab 2 838 Nov-29-2023, 09:07 PM
Last Post: john317ab
  .get() not retrieving value? Sedos101 2 577 Aug-25-2023, 11:48 AM
Last Post: deanhystad
  [Solved] Retrieving a pdf from sqlite3 BigMan 4 2,365 Mar-12-2022, 01:56 PM
Last Post: deanhystad
  Retrieving a column from a data set using a function Bayle 6 2,382 Oct-06-2021, 08:52 PM
Last Post: Bayle
  Retrieving Cookies whois1230 2 2,216 Nov-21-2020, 12:01 PM
Last Post: snippsat
  Problem: Retrieving Form data PythonDev 3 3,126 Oct-16-2020, 02:09 AM
Last Post: PythonDev
  Retrieving dictionary keys within with another dictionay bazcurtis 8 2,869 Oct-29-2019, 10:06 PM
Last Post: bazcurtis
  Retrieving items from JSON bazcurtis 12 5,089 Oct-27-2019, 05:18 PM
Last Post: bazcurtis
  Trouble retrieving dictionary from mysql.connector cursor swechsler 2 3,081 Sep-17-2019, 05:21 PM
Last Post: swechsler
  retrieving pvalue from statsmodels results Staph 4 3,054 Jul-18-2019, 03:27 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020