Search code examples
phphtmlfile-get-contentsfile-put-contents

How to echo a website page in php that has js file in it?


There is a site that I want to scrape: https://tse.ir/MarketWatch.html

I know that I have to use:

file_get_contents("https://examplesite.html")

to get the html part of site, but how can I find a specific part of site for example like this part in text file:

<td title="دالبر"title="something" class="txtclass="someclass">Tag namad">دالبر<Name</td>

When I open the text file, I never see this part and I think it is because in website there is JavaScript file. How can I get all information of website that include every part I want?


Solution

  • Content loaded by ajax request via javascript. This means you can't get this data simply grabbing the page contents.

    There are two ways of collecting data you need:

    1. Use solution based on selenium webdriver to load this page by real browser (which will execute JS), and collect data from rendered DOM.
    2. Research what kind of requests are sent by website to get this data. You could use network activity tab in browser dev tools. Here is example for chrome. For other browsers is the same or similar. Than you send the same request and pase response regarding to your needs.

    In your specific case, probably, you could use this url: https://tseest.ir/json/MarketWatch/data_211111.json to accees the json object with data you need.