理解爬虫原理

作业要求:

1. 简单说明爬虫原理

爬虫的原理 : 通过程序模拟浏览器请求站点,把站点返回的HTML代码、JSON数据、等爬到本地机器,从获取的数据中进行分析提取自己需要的数据,存放起来使用。

爬虫的流程:

    

2. 理解爬虫开发过程

  1).简要说明浏览器工作原理;

浏览器工作原理的实质就是实现http协议的通讯,就是向服务器发出请求,在浏览器窗口中展示想要访问的网络资源。资源的位置由用户使用 URI指定。使用get、post方法对浏览器进行发请求以及应答。

  

  2).使用 requests 库抓取网站数据;

url = 'http://news.gzcc.cn/html/2019/xiaoyuanxinwen_0328/11086.html'
res = requests.get(url)
res.encoding = 'utf-8'
s = BeautifulSoup(res.text,'html.parser')
s

  运行截图:

    

  3).了解网页

    写一个简单的html文件,包含多个标签,类,id

html_sample = ' 
<html> 
    <body> 
          <h1 id="title">Hello World!</h1> 
          <a href="www.baidu.com" class="link"> go to baidu</a>
          <p class="try">try to click</p>
    </body> 
</html> '

  4).使用 Beautiful Soup 解析网页;

    通过BeautifulSoup(html_sample,'html.parser')把上述html文件解析成DOM Tree

    select(选择器)定位数据

    找出含有特定标签的html元素

    找出含有特定类名的html元素

    找出含有特定id名的html元素

ss = BeautifulSoup(html_sample,'html.parser') ##通过BeautifulSoup(html_sample,'html.parser')把上述html文件解析成DOM Tree
title =ss.h1 ##找出含有特定标签的html元素
a = ss.select('a') ##select(选择器)定位数据
t = ss.select('#try') ##找出含有特定id名的html元素
p = ss.select('.link') ##找出含有特定类名的html元素
print(a)
print(t)
print(p)

    运行截图:

    

3.提取一篇校园新闻的标题、发布时间、发布单位、作者、点击次数、内容等信息。要求发布时间为datetime类型,点击次数为数值型,其它是字符串类型。

  url = http://news.gzcc.cn/html/2019/xiaoyuanxinwen_0328/11086.html

  •   校园新闻标题提取:
title = s.select('.show-title')[0].text
title

  运行截图:

    

  •   校园新闻发布时间提取(发布时间为datetime类型):
time1 = s.select('.show-info')[0].text.split()[0].split(':')[1]
time2 = s.select('.show-info')[0].text.split()[1]
time = time1+' '+time2
datetime.strptime(time,'%Y-%m-%d %H:%M:%S') 

  运行截图:

    

  •   校园新闻发布单位提取:
place = s.select('.show-info')[0].text.split()[4]
place.split('')[1]

   运行截图

    

  

  校园新闻作者提取:

name = s.select('.show-info')[0].text.split()[2]
name.split('')[1]

  运行截图:

    

  •   校园新闻点击次数提取:
clickUrl = 'http://oa.gzcc.cn/api.php?op=count&id=11086&modelid=80'
click = requests.get(clickUrl).text.split(';')[3]
c =click.split("'")[3]
type(c)
cs = int(c)
print(cs)

   运行截图:

    

  •   校园新闻内容提取
# 获取内容
content = s.select(".show-content")[0].text
print("内容为:",content)

    运行截图:

  • 提取新闻信息详细代码
from datetime import datetime
import bs4
import requests
from bs4 import BeautifulSoup

res=requests.get("http://news.gzcc.cn/html/2019/xiaoyuanxinwen_0328/11086.html")
res.encoding='utf-8'
s=bs4.BeautifulSoup(res.text,'html.parser')
# 获取新闻标题
title = s.select('.show-title')[0].text
# 获取发布时间
time1 = s.select('.show-info')[0].text.split()[0].split(':')[1]
time2 = s.select('.show-info')[0].text.split()[1]
time = time1+' '+time2
times = datetime.strptime(time,'%Y-%m-%d %H:%M:%S')
# 获取发布单位
place = s.select('.show-info')[0].text.split()[4]
places = place.split('')[1]
# 获取作者
name = s.select('.show-info')[0].text.split()[2]
names = name.split('')[1]
# 获取点击次数
clickUrl = 'http://oa.gzcc.cn/api.php?op=count&id=11086&modelid=80'
click = requests.get(clickUrl).text.split(';')[3]
c =click.split("'")[3]
type(c)
cs = int(c)
# 获取内容
content = s.select(".show-content")[0].text
# 输出
print("新闻标题:",title)
print("发布时间:",times)
print("发布单位",places)
print("作者:",names)
print("点击次数:",cs)
print("内容为:",content)

  运行截图:

原文地址:https://www.cnblogs.com/lawn/p/10622917.html