1. 简单说明爬虫原理
什么是爬虫
2. 理解爬虫开发过程
1).简要说明浏览器工作原理;
基本流程:
2).使用 requests 库抓取网站数据;
requests.get(url) 获取校园新闻首页html代码
import requests
from bs4 import BeautifulSoup
url=‘http://news.gzcc.cn/html/2019/tongzhigonggao_0321/11036.html‘
response=requests.get(url) #获取网页html
response.encoding=‘utf-8‘
print(response.text)
3).了解网页
写一个简单的html文件,包含多个标签,类,id
4).使用 Beautiful Soup 解析网页;
通过BeautifulSoup(html_sample,‘html.parser‘)把上述html文件解析成DOM Tree
select(选择器)定位数据
找出含有特定标签的html元素
找出含有特定类名的html元素
找出含有特定id名的html元素
3.提取一篇校园新闻的标题、发布时间、发布单位
url = ‘http://news.gzcc.cn/html/2019/xiaoyuanxinwen_0320/11029.html‘
import requests
from bs4 import BeautifulSoup
url=‘http://news.gzcc.cn/html/2019/tongzhigonggao_0321/11036.html‘
response=requests.get(url) #获取网页html
response.encoding=‘utf-8‘
print(response.text)
soup=BeautifulSoup(response.text,"lxml") #用BS4构建标签对象,用lxml解析器解析获取内容
print(soup.select(‘.show-title‘))#输出新闻标题
print(soup.select(‘.show-info‘))#输出新闻发布时间作者单位
原文:https://www.cnblogs.com/WYuHan/p/10599137.html