-1

I want to extract a URL from a remote PHP response. My current code gets the link variable response, but how can I only a grab the first line of the response, which in this case is a URL?

URL to look in first line of PHP response in link variable:

http://test.awebsite.com/1.m3u8?token=454766879809809

Python code:

req = urllib2.Request('http://www.somesite.com/test.php')
req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0')
response = urllib2.urlopen(req)   
link = response.read()

Full PHP response using print link:

10:01:08 T:7688  NOTICE: 
    http://test.awebsite.com/1.m3u8?token=454766879809809 
    <!-- Start -->
    <script type="text/javascript" src="http://1.js"></script>
    <script type="text/javascript" src="http://1.js"></script>
    <script type="text/javascript" src="http://3.js"></script>
    <noscript><br><center><font color='#000000' face='Verdana' style='font-size: 11px; background-color:#FFFFFF'><a target='_blank' href='http://www.ads.com'><font color='#000000'>ads</font></a></font></center></noscript>
    <!-- End -->
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
user1788736
  • 2,727
  • 20
  • 66
  • 110
  • 1
    The URL you ask for seems to be on the 2nd line. Check my updated answer for this.... – ant0nisk Nov 27 '15 at 18:10
  • Possible duplicate of [How do I split a mult-line string into multiple lines?](http://stackoverflow.com/questions/172439/how-do-i-split-a-mult-line-string-into-multiple-lines) – GingerPlusPlus Nov 27 '15 at 18:38

2 Answers2

0

You can try splitting the response by lines like this:

lines = link.split("\n")

And then get the first line:

anwser = lines[1] # 1st line is lines[0], 2nd is lines[1], 3rd is lines[2] etc...

Note that you should expect an exception to be raised if the substring you split at isn't in your string. One way you can check it like this:

first_line=""
if "\n" in link:
    first_line = link.split("\n")[1] # Gets the same result as before in one line...
else:
    print("Something went wrong...")
print(first_line)

Or with a try-except statement:

first_line=""
try:
    first_line = link.split("\n")[1]
except IndexError:
    print("Something went wrong...")
print(first_line)

I hope that answers your question!

GingerPlusPlus
  • 5,336
  • 1
  • 29
  • 52
ant0nisk
  • 581
  • 1
  • 4
  • 17
  • Thanks for reply . I tried the first two line of your code and i get this error:Error Contents: global name 'answer' is not defined – user1788736 Nov 27 '15 at 17:38
  • Are you using the anwser variable in an if or a loop? You might need to declare it before the if by writing: answer = "" Otherwise, if you try to use it outside this if/loop, you will get this error. – ant0nisk Nov 27 '15 at 17:41
  • See the updated anwser (the part with the if statement) for an example of what you might need to do... – ant0nisk Nov 27 '15 at 17:44
  • I edited my first post. i still getting empty data for first line. I am running this code in kodi and my print link shows response as first post(in kodi log). could you see the response and tell me why i can't grab that first line ? – user1788736 Nov 27 '15 at 18:11
  • @ant0nisk: *Note that it is a good practice to check if the character you split is in your string!* -- I'd argue otherwise. What's the point of writing more code to write out vague error message? – GingerPlusPlus Nov 27 '15 at 18:11
  • Yes, of course he could use a try-except statement. What I wanted to say here is that he should expect Exceptions to be raised in some cases. Sorry I didn't make that clear, I will edit my answer... – ant0nisk Nov 27 '15 at 18:15
  • Many thanks . The data was in second line as you mentioned it is working now ! In the browser it was showing in first line for some reason! – user1788736 Nov 27 '15 at 18:18
0

I decided to clarify some things missed in ant0nisk's answer by editing it, but I ended up completely rewriting it, so I decided to post it as separate answer.

You can try splitting the response by lines like this:

lines = link.splitlines()

And then get the first line:

anwser = lines[1] # 1st line is lines[0], 2nd is lines[1], 3rd is lines[2] etc...

Note that if link is made up from less than two lines, the line above raises raise IndexError exception, which (if unhandled) will terminate your program with message which will be meaningful for you, but might confuse non-programmers. If you for example want folks-friendly error message or have plan B what to do in case of one-line or empty request, you can handle the error with a try-except statement:

try:
    first_line = link.split("\n")[1]
except IndexError:
    print("Response is too short! (expected at least 2 lines)")
else: # Everything OK
    print(first_line)

Catching exception is expensive. Usually it is not a problem, because it happens when recovering from exceptional scenarios. But, if exception is raised often, so that handling it is not an exceptional scenario, using if instead of try-except might turn out to be faster.

Community
  • 1
  • 1
GingerPlusPlus
  • 5,336
  • 1
  • 29
  • 52