【问题标题】:How to copy content from a dynamic page using PHP?如何使用 PHP 从动态页面复制内容?
【发布时间】:2013-08-04 02:29:00
【问题描述】:

是否可以使用 PHP 获取下面给出的页面链接中显示的信息。我希望将页面上显示的所有文本内容复制到变量或文件中。

http://www.ncbi.nlm.nih.gov/nuccore/24655740?report=fasta&format=text

我也尝试过 cURL,但没有成功。 cURL 与我知道的其他一些网站一起工作的地方。但即使有 cURL 的解决方案也会发布。我可能已经尝试过各种可以使用 cURL 的方法。

【问题讨论】:

    标签: php bioinformatics ncbi


    【解决方案1】:

    使用 cURL 获取页面内容,然后对其进行解析 - 提取 <pre> 部分。

    $ch = curl_init();
    
    // Set query data here with the URL
    curl_setopt($ch, CURLOPT_URL, 'val=24655740&db=nuccore&dopt=fasta&extrafeat=0&fmt_mask=0&maxplex=1&sendto=t&withmarkup=on&log$=seqview&maxdownloadsize=1000000'); 
    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, '3');
    $content = trim(curl_exec($ch));
    curl_close($ch);
    // show ALL the content
    print $content;
    
    $start_index = strpos($content, '<pre>')+5;
    $end_index = strpos($content, '</pre>');
    $your_text = substr($content, $start_index, $end_index-$start_index);
    

    更新

    使用@ovitinho 的回答中的链接 - 现在可以使用了 :)

    【讨论】:

    • 这段代码对我不起作用...我只是得到一个灰色的背景。
    • @SRKR 使用来自 ovitinho 答案的链接 - 现在可以使用了 :)
    • 我用这个来获取它:function get_content($URL) { $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $URL); $data = curl_exec($ch); curl_close($ch); return $data; } 并使用了@ovitinho 的链接
    • @SRKR 是的,这几乎是完全相同的代码 - 它应该可以工作:)
    【解决方案2】:

    您需要通过 javascript 请求表单使用的 url 以显示此结果。

    我创建了这个最终网址

    http://www.ncbi.nlm.nih.gov/sviewer/viewer.fcgi?val=24655740&db=nuccore&dopt=fasta&extrafeat=0&fmt_mask=0&maxplex=1&sendto=t&withmarkup=on&log$=seqview&maxdownloadsize=1000000
    

    请注意在此请求的第一个链接中使用 24655740。

    你可以使用 cURL。

    【讨论】:

    • 您无法获取此内容,因为文本是通过 javascript 加载的(触发表单提交)
    • 那个链接太棒了,它就像魔术一样......我可以知道你从哪里得到那个链接......?
    • 我使用 chrome 元素检查器查看网络选项卡,您可以找到对服务器的请求。只需复制链接:)
    • 如果您需要其他网址,您可以通过这种方式轻松找到。打开你的chrome浏览器,输入你的url。按键盘上的 F12。然后在此窗口顶部找到网络选项卡。接下来,在底部寻找 XHR。他们重新加载您的页面以在此面板上查看结果。如果您觉得可以,请接受我的回答。谢谢
    猜你喜欢
    • 2022-01-09
    • 1970-01-01
    • 2022-08-17
    • 2016-02-29
    • 1970-01-01
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多