PHP作业:如何用file_get_contents获取POST搜索结果第二页?
How to Fetch the Second Page of Search Results via POST Request
Got it, let's work through this. You've nailed the first page with your POST request, so grabbing the second page just boils down to figuring out what pagination parameter the site uses and adding it to your POST data.
Here's the step-by-step breakdown:
Find the pagination parameter first
- Manually search for "catalogue" on the site, then click through to the second page of results.
- Open your browser's DevTools (press F12), head to the Network tab, and look for the POST request that loads the second page.
- Check the Form Data section of that request—you’ll spot an extra parameter that wasn’t in your first-page call (common ones are
page=2,p=2, oroffset=XXdepending on how the site paginates results).
Update your POST data with the pagination parameter
Let’s assume the site usespageas the pagination parameter (swap this out if your DevTools check shows a different key). Modify your$postdatato include this new parameter:
$postdata = http_build_query( array( 'q' => 'catalogue', 'submit' => 'searchbutton', 'page' => 2 // Add the pagination parameter here ) ); $opts = array('http' => array( 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata ) ); $context = stream_context_create($opts); $result = file_get_contents('http://wiki.webo-facto.com/', false, $context);
- Test the adjusted request
Run the modified code—this should pull the second page of your search results. If it doesn’t, double-check the exact parameter name and value from your DevTools capture (some sites usestartinstead ofpage, or expect an offset likeoffset=10if each page displays 10 items).
内容的提问来源于stack exchange,提问作者kizawa tomaru
相关产品推荐
相关产品推荐

