Python---实验九
1、使用標準庫urllib爬取“http://news.pdsu.edu.cn/info/1005/31269.htm”平頂山學(xué)院新聞網(wǎng)上的圖片,要求:保存到F盤pic目錄中,文件名稱命名規(guī)則為“本人姓名”+ “_圖片編號”,如姓名為張三的第一張圖片命名為“張三_1.jpg”。
from re import findall from urllib.request import urlopenurl = 'http://news.pdsu.edu.cn/info/1005/31269.htm' with urlopen(url) as fp:content=fp.read().decode('utf-8')pattern = '<img width="500" src="(.+?)"' #查找所有圖片鏈接地址 result = findall(pattern, content) #捕獲分組 #逐個讀取圖片數(shù)據(jù),并寫入本地文件 path='f:/pic/' name="煙雨" for index, item in enumerate(result):picture = 'http://news.pdsu.edu.cn/' + itemwith urlopen(str(picture)) as fp:with open(path+name+'_'+str(index+1)+'.png','wb') as fp1: #這里因為是從1開始,這里注意下fp1.write(fp.read())效果圖如下:
2、采用scrapy爬蟲框架,抓取平頂山學(xué)院新聞網(wǎng)(http://news.pdsu.edu.cn/)站上的內(nèi)容,具體要求:抓取新聞欄目,將結(jié)果寫入lm.txt。
cmd打開之后就別關(guān)了
scrapy startproject wsqwsq為項目名
cd wsq
scrapy genspider lm news.pdsu.edu.cnlm為爬蟲名稱,pdsu.edu.cn為爬取起始位置
分析:編寫正確的正則表達式篩選信息
由關(guān)鍵信息:<h2 class="fl">媒體平院</h2>
篩選其正則表達式如下:soup.find_all('h2', class_='fl')
找到lm.py也就是上面創(chuàng)建的爬蟲
編輯:將下面代碼負責(zé)粘貼下
pip install beautifulsoup4
pip install scrapy
倆第三方庫要安裝下
scrapy crawl lmlm為爬蟲名稱
效果圖如下:
3、采用request爬蟲模塊,抓取平頂山學(xué)院網(wǎng)絡(luò)教學(xué)平臺上的Python語言及應(yīng)用課程上的每一章標題(http://mooc1.chaoxing.com/course/206046270.html)。
cmd打開之后就別關(guān)了
scrapy startproject yyyy為項目名
cd yy
scrapy genspider beyond news.mooc1.chaoxing.com/course/206046270.htmlbeyond為爬蟲名稱,mooc1.chaoxing.com/course/206046270.html為爬取起始位置
分析:編寫正確的正則表達式篩選信息
由關(guān)鍵信息:<div class="f16 chapterText">第一章 python概述</div>
篩選其正則表達式如下:soup.findAll('div',class_='f16 chapterText')
找到beyond.py也就是上面創(chuàng)建的爬蟲
編輯:將下面代碼負責(zé)粘貼下
效果圖如下:
總結(jié)
以上是生活随笔為你收集整理的Python---实验九的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: “回风灭且起”下一句是什么
- 下一篇: 平院Python习题