爬取w3c课程—Urllib库使用
生活随笔
收集整理的這篇文章主要介紹了
爬取w3c课程—Urllib库使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
爬蟲原理
????? 瀏覽器獲取網頁內容的步驟:瀏覽器提交請求、下載網頁代碼、解析成頁面,爬蟲要做的就是:
簡單例子:利用Urllib庫爬取w3c網站教程
1、urllib的request模塊可以非常方便地抓取URL內容,也就是發送一個GET請求到指定的頁面,然后返回HTTP的響應:例如,對百度的一個w3c發送一個GET請求,并返回響應:
# coding:utf-8 import urllib.requestmy_url='https://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 = r'<a href="([\s\S]*?)" title=[\s\S]*?<h4>(.+)</h4>\n<p>([\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_url='https://www.w3cschool.cn/tutorial'#要獲取課程的網址def get_html(url):#訪問urlpage = urllib.request.urlopen(url)html = page.read().decode('utf-8')return htmlreg = r'<a href="([\s\S]*?)" title=[\s\S]*?<h4>(.+)</h4>\n<p>([\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)):#把課程名稱、課程簡介、課程鏈接寫到excel,python里面excel從0開始計算print (tutorial_list[i])運行,打印結果:
3、保存數據,保存數據到excel里面,用到excel第三方庫xlwt,也可以只用openpyxl,庫的使用可以參照官網:http://www.python-excel.org/
本次需要新建一個Excel,把課程名稱、課程簡介、課程鏈接寫到Excel里面,課程鏈接用xlwt.Formula設置超鏈接,Excel第一行設置為宋體,加粗,寫一些課程內容外的東西
import xlwt excel_path=r'tutorial.xlsx'#excel的路徑 book = xlwt.Workbook(encoding='utf-8', style_compression=0)# 創建一個Workbook對象,這就相當于創建了一個Excel文件 sheet = book.add_sheet('課程',cell_overwrite_ok=True)# 添加表 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)):#把課程名稱、課程簡介、課程鏈接寫到excel,python里面excel從0開始計算print (tutorial_list[i])sheet.write(i+1, 0, i+1)sheet.write(i+1, 1, tutorial_list[i][1])sheet.write(i+1, 2, tutorial_list[i][2])sheet.write(i+1, 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_path=r'tutorial.xlsx'#excel的路徑 my_url='https://www.w3cschool.cn/tutorial'#要獲取課程的網址 book = xlwt.Workbook(encoding='utf-8', style_compression=0)# 創建一個Workbook對象,這就相當于創建了一個Excel文件 sheet = book.add_sheet('課程',cell_overwrite_ok=True)# 添加表 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 = r'<a href="([\s\S]*?)" title=[\s\S]*?<h4>(.+)</h4>\n<p>([\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)):#把課程名稱、課程簡介、課程鏈接寫到excel,python里面excel從0開始計算print (tutorial_list[i])sheet.write(i+1, 0, i+1)sheet.write(i+1, 1, tutorial_list[i][1])sheet.write(i+1, 2, tutorial_list[i][2])sheet.write(i+1, 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
總結
以上是生活随笔為你收集整理的爬取w3c课程—Urllib库使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springmvc注解小示例(转)
- 下一篇: 封装axios的接口请求数据方法