建瓯企业网站建设,免费网站制作公司,吴江城乡和住房建设局网站,wordpress创建数据库错误爬虫原理 浏览器获取网页内容的步骤#xff1a;浏览器提交请求、下载网页代码、解析成页面#xff0c;爬虫要做的就是#xff1a; 模拟浏览器发送请求#xff1a;通过HTTP库向目标站点发起请求Request#xff0c;请求可以包含额外的header等信息#xff0c;等待服务器响应…爬虫原理 浏览器获取网页内容的步骤浏览器提交请求、下载网页代码、解析成页面爬虫要做的就是 模拟浏览器发送请求通过HTTP库向目标站点发起请求Request请求可以包含额外的header等信息等待服务器响应获取响应内容如果服务器正常响应会得到一个响应Response响应的内容便是所要获取的页面内容类型可能是HTML,Json字符串二进制数据图片或者视频等解析响应内容获取响应内容后解析各种数据如解析html数据正则表达式第三方解析库解析json数据json模块解析二进制数据:进一步处理或以wb的方式写入文件保存数据保存为文本数据库或者保存特定格式的文件简单例子利用Urllib库爬取w3c网站教程 1、urllib的request模块可以非常方便地抓取URL内容也就是发送一个GET请求到指定的页面然后返回HTTP的响应例如对百度的一个w3c发送一个GET请求并返回响应 # coding:utf-8
import urllib.requestmy_urlhttps://www.w3cschool.cn/tutorial#要获取课程的网址
page urllib.request.urlopen(my_url)
html page.read().decode(utf-8)
print(html) 把发送一个GET请求到指定的页面返回HTTP的响应写成一个函数 def get_html(url):#访问urlpage urllib.request.urlopen(url)html page.read().decode(utf-8)return html 将返回如下内容这与在浏览器查看源码看到的是一样的接下来可以根据返回的内容进行解析 2、利用正则表达式的分组提取课程名称、课程简介、课程链接导入python里面的re库 reg ra href([\s\S]*?) title[\s\S]*?h4(.)/h4\np([\s\S]*?)/p#运用正则表达式分组提取数据
reg_tutorial re.compile(reg)#编译一下正则表达式运行更快
tutorial_list reg_tutorial.findall(get_html(my_url))#进行匹配 到现在代码如下 # coding:utf-8
import urllib.request
import remy_urlhttps://www.w3cschool.cn/tutorial#要获取课程的网址def get_html(url):#访问urlpage urllib.request.urlopen(url)html page.read().decode(utf-8)return htmlreg ra href([\s\S]*?) title[\s\S]*?h4(.)/h4\np([\s\S]*?)/p#运用正则表达式分组提取数据
reg_tutorial re.compile(reg)#编译一下正则表达式运行更快
tutorial_list reg_tutorial.findall(get_html(my_url))#进行匹配print(一共有课程数 str(len(tutorial_list)))#打印出有多少课程for i in range(len(tutorial_list)):#把课程名称、课程简介、课程链接写到excelpython里面excel从0开始计算print (tutorial_list[i]) 运行打印结果 3、保存数据保存数据到excel里面用到excel第三方库xlwt也可以只用openpyxl库的使用可以参照官网http://www.python-excel.org/ 本次需要新建一个Excel把课程名称、课程简介、课程链接写到Excel里面课程链接用xlwt.Formula设置超链接Excel第一行设置为宋体加粗写一些课程内容外的东西 import xlwt
excel_pathrtutorial.xlsx#excel的路径
book xlwt.Workbook(encodingutf-8, style_compression0)# 创建一个Workbook对象这就相当于创建了一个Excel文件
sheet book.add_sheet(课程,cell_overwrite_okTrue)# 添加表
style xlwt.XFStyle()#初始化样式
font xlwt.Font()#创建字体
font.name 宋体#指定字体名字
font.bold True#字体加粗
style.font font#将该font设定为style的字体
sheet.write(0, 0, 序号,style)#用之前的style格式写第一行行、列从0开始计算
sheet.write(0, 1, 课程,style)
sheet.write(0, 2, 简介,style)
sheet.write(0, 3, 课程链接,style) 写课程内容到Excel for i in range(len(tutorial_list)):#把课程名称、课程简介、课程链接写到excelpython里面excel从0开始计算print (tutorial_list[i])sheet.write(i1, 0, i1)sheet.write(i1, 1, tutorial_list[i][1])sheet.write(i1, 2, tutorial_list[i][2])sheet.write(i1, 3, xlwt.Formula(HYPERLINK( https: tutorial_list[i][0])))#把链接写进去并用xlwt.Formula设置超链接book.save(excel_path)#保存到excel Excel内容 全部代码如下 # coding:utf-8
import urllib.request
import re
import xlwt
excel_pathrtutorial.xlsx#excel的路径
my_urlhttps://www.w3cschool.cn/tutorial#要获取课程的网址
book xlwt.Workbook(encodingutf-8, style_compression0)# 创建一个Workbook对象这就相当于创建了一个Excel文件
sheet book.add_sheet(课程,cell_overwrite_okTrue)# 添加表
style xlwt.XFStyle()#初始化样式
font xlwt.Font()#创建字体
font.name 宋体#指定字体名字
font.bold True#字体加粗
style.font font#将该font设定为style的字体
sheet.write(0, 0, 序号,style)#用之前的style格式写第一行行、列从0开始计算
sheet.write(0, 1, 课程,style)
sheet.write(0, 2, 简介,style)
sheet.write(0, 3, 课程链接,style)def get_html(url):#访问urlpage urllib.request.urlopen(url)html page.read().decode(utf-8)return htmlreg ra href([\s\S]*?) title[\s\S]*?h4(.)/h4\np([\s\S]*?)/p#运用正则表达式分组提取数据
reg_tutorial re.compile(reg)#编译一下正则表达式运行更快
tutorial_list reg_tutorial.findall(get_html(my_url))#进行匹配print(一共有课程数 str(len(tutorial_list)))#打印出有多少课程for i in range(len(tutorial_list)):#把课程名称、课程简介、课程链接写到excelpython里面excel从0开始计算print (tutorial_list[i])sheet.write(i1, 0, i1)sheet.write(i1, 1, tutorial_list[i][1])sheet.write(i1, 2, tutorial_list[i][2])sheet.write(i1, 3, xlwt.Formula(HYPERLINK( https: tutorial_list[i][0])))#把链接写进去并用xlwt.Formula设置超链接book.save(excel_path)#保存到excel 转载于:https://www.cnblogs.com/fish-dream/p/10560010.html