2017-09-16 43 views
0

所以我不想拉整个页面,只是页面的前40KB。就像这个Facebook Debugger工具一样。如何通过cURL只抓取页面的第一个40KB

我的目标就是抓住社交媒体元数据,即og:image

可以在任何编程语言,PHP或者Python。

我确实有phpQuery代码,使用的file_get_contents /卷曲,我知道如何分析接收到的HTML,我的问题是“如何在没有获取整个页面抓取网页的只有第一NKB”

+0

也许这将帮助https://stackoverflow.com/a/12014561/661872 –

+0

@LawrenceCherone我在phpQuery中有使用file_get_contents/cURL的代码,并且我知道如何解析收到的HTML,我的问题是**“如何仅抓取页面的第一个nKB而不抓取整个页面”** – Umair

+2

这似乎已经回答[这里](https://stackoverflow.com/questions/2032924/how-to-partially-download-a-remote-file-with-curl)。 – Dardanboy

回答

3

这不具体到Facebook或任何其他社交媒体网站,但你可以得到前40 KB和Python这样的:

import urllib2 
start = urllib2.urlopen(your_link).read(40000) 
+0

这是否会停止加载页面,只要前40 KB ? – Umair

+0

@Umair它只会先读取40KB。所以,是的,之后就会停止。 – mdegis

0

这可以用于:

curl -r 0-40000 -o 40k.raw https://www.keycdn.com/support/byte-range-requests/ 

-r代表范围:

来自卷边手册页:

r, --range <range> 
      (HTTP FTP SFTP FILE) Retrieve a byte range (i.e a partial document) from a HTTP/1.1, FTP or SFTP server or a local FILE. Ranges can be 
      specified in a number of ways. 

      0-499  specifies the first 500 bytes 

      500-999 specifies the second 500 bytes 

      -500  specifies the last 500 bytes 

      9500-  specifies the bytes from offset 9500 and forward 

      0-0,-1 specifies the first and last byte only(*)(HTTP) 

更多信息可以在本文中找到:https://www.keycdn.com/support/byte-range-requests/

以防万一,这是一个基本的例子如何与go

package main 

import (
    "fmt" 
    "io" 
    "io/ioutil" 
    "log" 
    "net/http" 
) 

func main() { 
    response, err := http.Get("https://google.com") 
    if err != nil { 
     log.Fatal(err) 
    } 
    defer response.Body.Close() 
    data, err := ioutil.ReadAll(io.LimitReader(response.Body, 40000)) 
    fmt.Printf("data = %s\n", data) 
}