Scraping 'N' pages with Beautifulsoup and Requests (How to obtain the true page number)

梦想与她 提交于 2020-01-14 05:57:09

问题


I want to get all the titles() in the website.

http://www.shyan.gov.cn/zwhd/web/webindex.action

Now, my code successfully scrapes only one page. However, there are multiple pages available at the site above in which I would like to to scrape.

For example, with the url above, when I click the link to "page 2", the overall url does NOT change. I looked at the page source and saw javascript code to advance to the next page like this: javascript:gotopage(2) or javascript:void(0). My code is here (get page 1)

from bs4 import Beautifulsoup
import requests
url = 'http://www.shyan.gov.cn/zwhd/web/webindex.action'
r =  requests.get(url)
soup = Beautifulsoup(r.content,'lxml')
titles = soup.select('td.tit3 > a')
for title in titles:
    print(title.get_text())

How can my code be changed to scrape titles from all the available listed pages? Thank you very much!


回答1:


Try to use the following URL format:

http://www.shiyan.gov.cn/zwhd/web/webindex.action?keyWord=&searchType=3&page.currentpage=2&page.pagesize=15&page.pagecount=2357&docStatus=&sendOrg=

The site is using javascript to pass hidden page information to the server to request the next page. When you view the source you will find:

<form action="/zwhd/web/webindex.action" id="searchForm" name="searchForm" method="post">
 <div class="item">
     <div class="titlel">
      <span>留言查询</span>
     <label class="dow"></label>
     </div>
     <input type="text" name="keyWord" id="keyword" value="" class="text"/>
     <div class="key">
        <ul>
            <li><span><input type="radio" checked="checked" value="3" name="searchType"/></span><p>编号</p></li>
            <li><span><input type="radio" value="2" name="searchType"/></span><p>关键字</p></li>
        </ul>    
     </div>
     <input type="button" class="btn1" onclick="search();" value="查询"/>
  </div>
  <input type="hidden" id="pageIndex" name="page.currentpage" value="2"/>
  <input type="hidden" id="pageSize" name="page.pagesize" value="15"/>
  <input type="hidden" id="pageCount" name="page.pagecount" value="2357"/>
  <input type="hidden" id="docStatus" name="docStatus" value=""/>
  <input type="hidden" id="sendorg" name="sendOrg" value=""/>
  </form>


来源:https://stackoverflow.com/questions/36684949/scraping-n-pages-with-beautifulsoup-and-requests-how-to-obtain-the-true-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!