最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How To Get HTML Eelement From Another Page

programmeradmin0浏览0评论

I'm building a site that provides Windows software that users can download. I want to retrieve text on another page.

Software Review Page (e.g: domain/obs-studio/):

<ul class="software_facts">
    <li>
        <div class="dorat">
            <div class="labelimg">[thumbnail]</div> 
            <div class="wp-block-button is-style-squared">
                <form name="myform1" action="/download-page/" method="post"><input name="bode" type="hidden" value=".0.8/OBS-Studio-25.0.8-Full-Installer-x64.exe"></form>
                <div class="wp-block-button__link has-background has-vivid-green-cyan-background-color" onclick="document.myform1.submit()">Download (24 MB)</div>
            </div>
        </div>
    </li>
    <li class="bg">
        <p class="labelnil">Nilai:</p>
        [kkratings]
    </li>
    <li>
        <p class="label">Version:</p>
        <p itemprop="softwareVersion">25.0.8</p>
    </li>
    <li class="bg">
        <p class="label">Publisher:</p>
        <p itemprop="publisher" itemscope="" itemtype=""><span itemprop="name">Jim</span></p>
    </li>
    <li>
        <p class="label">Sistem Operasi:</p>
        <p itemprop="operatingSystem">Windows</p>
    </li>
    <li class="bg">
        <p class="label">Kategori Aplikasi:</p>
        <p itemprop="applicationCategory">Multimedia</p>
    </li>
    <li>
        <p class="label">Licence:</p>
        <p>Freeware</p>
    </li>
</ul>

The user is directed to the domain/download-page/ page when clicking on the "Download (24MB)" button.

Download Page (e.g. domain/download-page/):

<p>OBS Studio was developed by <strong>Jim</strong>, the latest version is <strong>25.0.8</strong>.</p>

I want the "Jim" and "25.0.8" sections to change according to the software information that the user wants to download on the previous page.

I use Wordpress, how do I do this with Javascript or PHP. Can anyone help me, thanks in advance.

I'm building a site that provides Windows software that users can download. I want to retrieve text on another page.

Software Review Page (e.g: domain/obs-studio/):

<ul class="software_facts">
    <li>
        <div class="dorat">
            <div class="labelimg">[thumbnail]</div> 
            <div class="wp-block-button is-style-squared">
                <form name="myform1" action="/download-page/" method="post"><input name="bode" type="hidden" value="https://github/obsproject/obs-studio/releases/download/25.0.8/OBS-Studio-25.0.8-Full-Installer-x64.exe"></form>
                <div class="wp-block-button__link has-background has-vivid-green-cyan-background-color" onclick="document.myform1.submit()">Download (24 MB)</div>
            </div>
        </div>
    </li>
    <li class="bg">
        <p class="labelnil">Nilai:</p>
        [kkratings]
    </li>
    <li>
        <p class="label">Version:</p>
        <p itemprop="softwareVersion">25.0.8</p>
    </li>
    <li class="bg">
        <p class="label">Publisher:</p>
        <p itemprop="publisher" itemscope="" itemtype="http://schema/Organization"><span itemprop="name">Jim</span></p>
    </li>
    <li>
        <p class="label">Sistem Operasi:</p>
        <p itemprop="operatingSystem">Windows</p>
    </li>
    <li class="bg">
        <p class="label">Kategori Aplikasi:</p>
        <p itemprop="applicationCategory">Multimedia</p>
    </li>
    <li>
        <p class="label">Licence:</p>
        <p>Freeware</p>
    </li>
</ul>

The user is directed to the domain/download-page/ page when clicking on the "Download (24MB)" button.

Download Page (e.g. domain/download-page/):

<p>OBS Studio was developed by <strong>Jim</strong>, the latest version is <strong>25.0.8</strong>.</p>

I want the "Jim" and "25.0.8" sections to change according to the software information that the user wants to download on the previous page.

I use Wordpress, how do I do this with Javascript or PHP. Can anyone help me, thanks in advance.

Share Improve this question asked Jun 6, 2020 at 2:43 R.M. RezaR.M. Reza 1417 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You're talking about scraping, which is generally an unreliable method of getting data, especially if you don't control the other page.

However, in PHP, once you retrieve the download page with something like wp_remote_get, you can use a regular expression to extract the pieces you need with preg_match_all.

$response = wp_remote_get( 'domain/download-page/' );
$page = wp_remote_retrieve_body( $response );
$regexp = '/developed by <strong>(.*?)<\/strong>.*?version is <strong>(.*?)<\/strong>/';
preg_match( $regexp, $page, $matches );
echo $matches[1]; // Jim
echo $matches[2]; // 25.0.8

Basically, each of the (.*?) parts captures the content that appears in that part of the pattern, and puts it in an array called $matches. Note that any change to how the developer and version are presented on the download page will likely break this pattern, so be careful.

发布评论

评论列表(0)

  1. 暂无评论