使用 PHP 抓取完整图像 src
我正在尝试用 php 抓取 img src,我可以很好地获取 src,但是如果 src 不包含完整路径,那么我无法真正重用它。 有没有办法使用php获取图像的完整路径(如果使用右键菜单,浏览器可以获取它)。
IE。 如何获取包含以下两个示例之一中的域的完整路径?
src="../foo/logo.png"
src="/images/logo.png"
谢谢,
艾伦
I am trying to scrape img src's with php, I can get the src fine, but if the src does not include the full path then I can't really reuse it. Is there a way to grab the full path of the image using php (browsers can get it if you use the right click menu).
ie. How do I get a FULL path including the domain in one of the following two examples?
src="../foo/logo.png"
src="/images/logo.png"
Thanks,
Allan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要正则表达式...只需要一些耐心。 我真的不想为你编写代码,只是检查 src 是否以
http://
开头,如果不是,你就会有 3 种不同的情况。/
开头,则在前面添加 http://domain.com..
你必须拆分 完整的 URL 和 破解片段 直到 src 开始带有/
或者....偷懒并
从 http://www.web- max.ca/PHP/misc_24.php
You don't need a regex... just some patience. I don't really want to write the code for you, but just check if the src starts with
http://
, and if not, you have like 3 different cases./
then prepend http://domain.com..
you'll have to split the full URL and hack off pieces until the src starts with a/
Or.... be lazy and steal this script
From http://www.web-max.ca/PHP/misc_24.php
除非您有开始的站点 URL(在这种情况下,您可以将其添加到 src 属性的值之前),否则您似乎只剩下一个字符串。
当然,我假设您无权访问任何其他信息。 如果您正在解析 HTML,我假设您必须能够访问至少 HTML 页面的绝对 URL,但也许不能。
Unless you have the site URL you're starting with (in which case you can prepend it to the value of the src attribute) it seems like all you're left with there is a string.
I'm assuming you don't have access to any additional information of course. If you're parsing HTML, I'd assume you must be able to access an absolute URL to at least the HTML page, but perhaps not.