使用Beautifulsoup和请求刮取'N'页面(如何获取真实的页码)

时间:2016-04-18 03:21:53

标签: python selenium beautifulsoup python-requests scrape

我想获得网站上的所有标题()。

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

现在,我的代码只成功抓取了一页。但是,上面的网站上有多个页面可供我查阅。

例如,使用上面的网址,当我点击“第2页”链接时,整个网址不会更改。我查看了页面源代码并看到了javascript代码,以便进入下一页,如下所示:javascript:gotopage(2)或javascript:void(0)。 我的代码在这里(获取第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())

如何更改我的代码以从所有可用列表页面中删除标题? 非常感谢你!

1 个答案:

答案 0 :(得分:1)

尝试使用以下网址格式:

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

该网站使用javascript将隐藏的页面信息传递给服务器以请求下一页。当您查看来源时,您会发现:

<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>