简体   繁体   English

如何使用 Python 从远程 PHP 响应字符串中获取第一行 url?

[英]How to grab first line url from a remote PHP response string using Python?

I want to extract a URL from a remote PHP response.我想从远程 PHP 响应中提取 URL。 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?

URL to look in first line of PHP response in link variable:在链接变量中查看 PHP 响应第一行的 URL:

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 :使用print link完整 PHP 响应:

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 -->

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:或者使用 try-except 语句:

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

I hope that answers your question!我希望能回答你的问题!

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.我决定通过编辑来澄清ant0nisk 的答案遗漏的一些内容,但我最终完全重写了它,所以我决定将其作为单独的答案发布。

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.请注意,如果link由少于两行组成,则上面的行会引发引发IndexError异常,该异常(如果未处理)将终止您的程序并显示对您有意义的消息,但可能会使非程序员感到困惑。 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:例如,如果您想要人们友好的错误消息或计划 B 在单行或空请求的情况下做什么,您可以使用 try-except 语句处理错误:

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.但是,如果经常引发异常,因此处理它不是一个例外情况,使用if代替try-except可能会更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM