参考《Python编程快速上手——上繁琐工作自动化》第11章中的例子,打开男神个人博客页面中的链接。京东和百度的页面貌似都做了反爬措施,小白只好找最好爬的页面进行练习,看到博文后又心动了一下下。
以下是页面html
<ul class="post-list">
<li>
<span class="post-meta">Nov 12, 2018</span>
<h2>
<a class="post-link" href="/tech/2018/03/07/Reinforcement-Learning.html">笔记:Reinforcement Learning</a>
</h2>
</li>
<li>
<span class="post-meta">Jun 12, 2018</span>
<h2>
<a class="post-link" href="/tech/2018/03/05/tensorflow-learning.html">笔记:TensorFlow框架</a>
</h2>
</li>
</ul>
step 1: 用request.get()接收目标页面的url
step 2:用bs4.Beautifiulsoup()解析页面html代码
step 3:用select()方法解析html标签,获得页面中链接所在的标签行,得到包含多个标签行的数组。
step 4:用get方法得到标签行数组中每个标签行中href属性后跟着的链接
step 5:用webbrowser.open()打开链接
import requests,sys,webbrowser,bs4,re