Python---实验九

1、使用标准库urllib爬取“http://news.pdsu.edu.cn/info/1005/31269.htm”平顶山学院新闻网上的图片,要求:保存到F盘pic目录中,文件名称命名规则为“本人姓名”+ “_图片编号”,如姓名为张三的第一张图片命名为“张三_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)  #捕获分组
#逐个读取图片数据,并写入本地文件
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爬虫框架,抓取平顶山学院新闻网(http://news.pdsu.edu.cn/)站上的内容,具体要求:抓取新闻栏目,将结果写入lm.txt。

cmd打开之后就别关了
scrapy startproject wsqwsq为项目名
cd wsq
scrapy genspider lm news.pdsu.edu.cnlm为爬虫名称,pdsu.edu.cn为爬取起始位置
在这里插入图片描述
分析:编写正确的正则表达式筛选信息
由关键信息:<h2 class="fl">媒体平院</h2>
筛选其正则表达式如下:soup.find_all('h2', class_='fl')
找到lm.py也就是上面创建的爬虫
编辑:将下面代码负责粘贴下
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爬虫模块,抓取平顶山学院网络教学平台上的Python语言及应用课程上的每一章标题(http://mooc1.chaoxing.com/course/206046270.html)。

cmd打开之后就别关了
scrapy startproject yyyy为项目名
cd yy
scrapy genspider beyond news.mooc1.chaoxing.com/course/206046270.htmlbeyond为爬虫名称,mooc1.chaoxing.com/course/206046270.html为爬取起始位置
在这里插入图片描述
分析:编写正确的正则表达式筛选信息
由关键信息:<div class="f16 chapterText">第一章 python概述</div>
筛选其正则表达式如下:soup.findAll('div',class_='f16 chapterText')
找到beyond.py也就是上面创建的爬虫
编辑:将下面代码负责粘贴下

# -*- 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)

效果图如下:
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/379095.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Request.Url学习(转)

原文地址&#xff1a;http://www.cnblogs.com/jame-peng1028/articles/1274207.html?login1#commentform 网址&#xff1a;http://localhost:1897/News/Press/Content.aspx/123?id1#tocRequest.ApplicationPath/Request.PhysicalPathD:\Projects\Solution\web\News\Press\Con…

32接上拉5v_51单片机P0口上拉电阻的选择

作为I/O口输出的时候时&#xff0c;输出低电平为0 输出高电平为高组态(并非5V&#xff0c;相当于悬空状态&#xff0c;也就是说P0 口不能真正的输出高电平)。给所接的负载提供电流&#xff0c;因此必须接(一电阻连接到VCC)&#xff0c;由电源通过这个上拉电阻给负载提供电流。P…

[转载]FPGA/CPLD重要设计思想及工程应用(时序及同步设计)

来源&#xff1a;http://www.eetop.cn/blog/html/11/317611-13412.html 数字电路中,时钟是整个电路最重要、最特殊的信号。 第一, 系统内大部分器件的动作都是在时钟的跳变沿上进行, 这就要求时钟信号时延差要非常小, 否则就可能造成时序逻辑状态出错. 第二, 时钟信号通常是系统…

duration java_Java Duration类| ofMinutes()方法与示例

duration javaDuration Class of Minutes()方法 (Duration Class ofMinutes() method) ofMinutes() method is available in java.time package. ofMinutes()方法在java.time包中可用。 ofMinutes() method is used to represent the given minutes value in this Duration. of…

实验五 图形设计

每复制一个方法都要绑定Paint事件 一、创建Windows窗体应用程序&#xff0c;要求如下&#xff1a;&#xff08;源代码运行界面&#xff0c;缺少任一项为0分&#xff0c;源代码只需粘贴绘制图形代码所在的方法&#xff0c;不用粘贴太多&#xff09; 例如: &#xff08;1&…

yuv编码成h264格式写成文件

yuv编码成h264格式写成文件 &#xff08;使用ffmpeg 编码yuv420p编码成h264格式&#xff09; #include <stdio.h> #include <stdlib.h> #include <stdint.h>#include <libavcodec/avcodec.h> #include <libavutil/time.h> #include <libavut…

c++ stl队列初始化_声明,初始化和访问向量| C ++ STL

c stl队列初始化Here, we have to declare, initialize and access a vector in C STL. 在这里&#xff0c;我们必须声明&#xff0c;初始化和访问C STL中的向量。 向量声明 (Vector declaration) Syntax: 句法&#xff1a; vector<data_type> vector_name;Since, vec…

ADO.NET与SQL Server数据库的交互

7.3.1 使用SqlConnection对象连接数据库 例如&#xff1a;建立与SQL Server数据库的连接。 string connstring"Data Sourceservername;uidusername;pwdpassword;Initial Catalogdbname";SqlConnection connnew SqlConnection(connstring);conn.Open(); 例如&#xf…

nsis 修改exe执行权限

通过修改注册表的方式&#xff0c;修改exe的执行权限。&#xff0c;以下例子是使用管理员运行。 ;添加admin权限 SectionWriteRegStr HKCU "SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" "$INSTDIR\spp.exe" "RUNASADMIN&qu…

linux ftp日志_linux学习笔记(一)——Linux分区和目录结构

linux学习笔记&#xff08;一&#xff09;——Linux分区和目录结构安装Linux时&#xff0c;手动挂载分区的情况下&#xff0c;/ 和 swap 是必须要挂载的&#xff0c;其他/home、/boot 等可以根据需要自行挂载。一般来说&#xff0c;简单的话&#xff0c;建议挂载三个分区&#…

C#通过VS连接MySQL数据库实现增删改查基本操作

创建一个数据库wsq 里面有一张beyondyanyu表 表里面有id(int)、names(varchar)、count(int)、passwords(varchar) 数据可以自己添 1、导入MySQL引用&#xff0c;你需要从官网或者其他地方下载&#xff0c;私聊我也可以 using MySql.Data.MySqlClient; 2、创建MySqlConnection对…

使用ffmpeg的filter处理yuv数据包括split filter(分流)、crop filter(裁剪)、vflip filter(垂直向上的翻转)、overlay filter(合成)

使用ffmpeg的filter处理yuv数据包括split filter(分流)、crop filter(裁剪)、vflip filter(垂直向上的翻转)、overlay filter(合成) #include <stdio.h>#include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libavfilter/avfil…

vc++ 6.0 堆栈_在C ++中使用链接列表实现堆栈

vc 6.0 堆栈To implement a stack using a linked list, basically we need to implement the push() and pop() operations of a stack using linked list. 要使用链接列表实现堆栈 &#xff0c;基本上&#xff0c;我们需要使用链接列表实现堆栈的push()和pop()操作。 Exampl…

烟雨小书店

烟雨小书店演示视频 源码

协议地址结构_TCP/IP 协议 讲解

计算机网络体系结构分层太厉害了&#xff0c;终于有人能把TCP/IP 协议讲的明明白白了计算机网络体系结构分层不难看出&#xff0c;TCP/IP 与 OSI 在分层模块上稍有区别。OSI 参考模型注重“通信协议必要的功能是什么”&#xff0c;而 TCP/IP 则更强调“在计算机上实现协议应该开…

ffmpeg进行混音,将两路音频pcm数据合成一路输出

ffmpeg进行混音&#xff0c;将两路音频pcm数据合成一路输出 audiomixer.h #ifndef AUDIOMIXER_H #define AUDIOMIXER_H#include <map> #include <mutex> #include <cstdio> #include <cstdint> #include <string> #include <memory>exter…

python sep函数_Python中带有print()函数的sep参数

python sep函数sep parameter stands for separator, it uses with the print() function to specify the separator between the arguments. sep参数代表分隔符&#xff0c;它与print()函数一起使用以指定参数之间的分隔符。 The default value is space i.e. if we dont us…

关于 MySQL 主从复制的配置(转)

来源&#xff1a;http://www.oschina.net/bbs/thread/10388设置Mysql的主从设置很重要&#xff0c;有如下几点用处&#xff1a;1 做备份机器&#xff0c;一旦主服务器崩溃&#xff0c;可以直接启用从服务器作为主服务器2 可以直接锁定从服务器的表只读&#xff0c;然后做备份数…

Silverlight 同域WCF免跨域文件

在sl3使用wcf时常常会因为sl中调用了不同域的wcf服务而导至调用服务失败&#xff0c;记得在很久以前sl当是只支持同域的访问&#xff0c;那么让我有一个想法&#xff0c;就是在sl引用时可以动态地取得当前sl所在的域&#xff0c;而wcf服务也必须同时部署到这个域下边&#xff0…