日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

Python---实验九

發(fā)布時間:2023/12/1 python 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python---实验九 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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
倆第三方庫要安裝下

# -*- coding: utf-8 -*- import scrapy from bs4 import BeautifulSoup import re class LmmSpider(scrapy.Spider):name = 'lmm'allowed_domains = ['pdsu.cn']start_urls = ['http://news.pdsu.edu.cn/']def parse(self, response):html_doc=response.textsoup= BeautifulSoup(html_doc, 'html.parser') re=soup.find_all('h2', class_='fl')content=''for lm in re:print(lm.text)content+=lm.text+'\n'with open('f:\\lm.txt', 'a+') as fp:fp.writelines(content)#保存路徑可變

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é)粘貼下

# -*- coding: utf-8 -*- import scrapy import re import requests import bs4headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36' }url='http://mooc1.chaoxing.com/course/206046270.html' response = requests.get(url,headers=headers).text soup = bs4.BeautifulSoup(response,'html.parser') t=soup.findAll('div',class_='f16 chapterText') for ml in t:print (ml.text)

效果圖如下:

總結(jié)

以上是生活随笔為你收集整理的Python---实验九的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。