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