python3 — 基于requests + beautifulsoup 实现网页信息的抓取
前面我已经在博客中已经讲解了 requests 、 beautifulsoup 库的具体使用方法。
如果还不懂可以去我的博客园瞧一瞧,下面就是两个库的超链接,可点开学习!
接下来我们就一起来实战练习下
一、项目简述:
1.内容:抓取广东工业大学华立学院图书馆官网的新书推荐,保存所有新书的书名。
2.目标网站:http://library.hualixy.com/index.php?g=portal&m=list&a=index&id=79
3.编程软件:pycharm(社区、专业版均可)
4.使用 python 3.7版本
5.涉及的 python库名称:requests 、 beautifulsoup
二、具体方法:
1.导入库
import requests
from bs4 import BeautifulSoup
2.简单分析目标网站,获得请求头,防止爬虫被网页隔断。

3.找到了请求头,就可以用requests.get ( ) 方法后可以伪装浏览器登陆啦。
#循环访问所有页数 number
def get_html(number):
#伪装浏览器访问
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
url = 'http://library.hualixy.com/index.php?g=Portal&m=List&a=index&id=79&p='+str(number)
response = requests.get(url,headers = headers)
response.encoding =

最低0.47元/天 解锁文章
2271

被折叠的 条评论
为什么被折叠?



