使用php从URL获取内容 [英] Get content from a url using php

查看:235
本文介绍了使用php从URL获取内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从特定的网址获取动态内容:

I want to get the dynamic contents from a particular url:

我已经使用了代码

echo $content=file_get_contents('http://www.punoftheday.com/cgi-bin/arandompun.pl');

我得到以下结果:

document.write('"Bakers have a great knead to make bread."

') document.write('© 1996-2007 Pun of the Day.com
')

我该如何获得字符串?面包师们非常擅长制作面包. 只有第一个document.write中的字符串会更改,其他代码将保持不变

How can i get the string Bakers have a great knead to make bread. Only string inside first document.write will change, other code will remain constant

此致

Pankaj

推荐答案

您正在获取一个JavaScript代码段,该代码段应直接内置在文档中,而不是由脚本查询.里面的代码是JavaScript.

You are fetching a JavaScript snippet that is supposed to be built in directly into the document, not queried by a script. The code inside is JavaScript.

您可以使用正则表达式提取代码,但我建议不要这样做.首先,这样做可能不合法.其次,它们提供的数据格式可以随时更改,从而破坏了脚本.

You could pull out the code using a regular expression, but I would advise against it. First, it's probably not legal to do. Second, the format of the data they serve can change any time, breaking your script.

我认为您应该访问 其RSS feed .您可以以编程方式比JavaScript进行解析.

I think you should take at their RSS feed. You can parse that programmatically way easier than the JavaScript.

查看有关如何执行此操作的问题: 使用PHP解析RSS/Atom提要的最佳方法

Check out this question on how to do that: Best way to parse RSS/Atom feeds with PHP

这篇关于使用php从URL获取内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆