Python爬虫之初体验

news/2024/7/19 11:48:34 标签: 爬虫, python

Python爬虫,一般用于抓取特定的内容,最近想学学,通过网络抓取自己想要的内容,于是乎学习了一下Python,用一个小案例来纪念一下学习的成果。

案例程序主要功能:抓取我们学校校园网新闻中的图片

#coding=utf-8
import urllib
import re
# 定义个函数 抓取网页内容
def getHtml(url):
    webPage = urllib.urlopen(url)
    html = webPage.read()
    return html

# 定义一个函数 抓取网页中的图片
def getNewsImgs(html):
    # 正则表达式 
    reg = r'src="(.+?\.jpg)"'
    img = re.compile(reg)
    # 获取网页中所有符合条件的图片url
    imglist = re.findall(img,html)
    x = 0
    # 根据图片地址下载图片并重命名
    for imgUrl in imglist:
        urllib.urlretrieve("http://www.abc.edu.cn/news/"+imgUrl,'news-%s.jpg' % x)
        x += 1

# 获取网页
html = getHtml("http://www.abc.edu.cn/news/show.aspx?id=21413&cid=5")
# 抓取图片
print getNewsImgs(html)

效果:成功抓取了新闻中的两张图片O(∩_∩)O~

img_4871c5828f7b3039ad29197b997c9020.gif
爬虫抓图片.gif

http://www.niftyadmin.cn/n/1044653.html

相关文章

Ansible 进阶技巧

原文 http://www.ibm.com/developerworks/cn/linux/1608_lih_ansible/index.html?cadrs-简介 Ansible 是一个系统自动化工具,可以用来做系统配管理,批量对远程主机执行操作指令。我自己使用 Ansible 也有一段时间了,这里总结了一些使用 Ans…

【从零开始学习 SystemVerilog】10.3、SystemVerilog 杂项—— Coverpoint Bins

bins构造允许为覆盖点变量的给定可能值范围内的每个值创建单独的bin。 文章目录 用法ExamplesAutomatic Bins固定数量的 automatic bins在给定范围内拆分固定数量的bins用法 coverpoint mode {// Manually create a separate bin for each valuebins zero = {0};bins one = {…

【从零开始学习 SystemVerilog】11.1、SystemVerilog 断言—— Assertions Introduction(断言概述)

系统的行为可以写为断言,该断言在任何时候都应该为真。因此,断言用于验证定义为属性的系统的行为,也可用于功能覆盖。 文章目录 设计的属性是什么?为什么我们需要断言?断言语句的类型断言的构建块SequencePropertyImmediate Assertion(立即断言)Concurrent Assertions(…

ps grep awk 结合 xargs kill进程

ps -ef|grep "node client"|grep -v grep|awk {print $2}|xargs kill -15 转载于:https://www.cnblogs.com/xunux/p/7001112.html

【从零开始学习 SystemVerilog】11.2、SystemVerilog 断言—— Immediate Assertions(立即断言)

立即断言是基于仿真事件语义执行的,需要在过程块中指定。在仿真过程中,它的处理方式与if语句中的表达式相同。 如果在执行语句时表达式为true,则立即断言将通过,如果表达式的计算结果为false(X、Z或0),则立即断言会失败。这些断言用于仿真,不适用于正式验证。它可以用…

无边框窗口拖动

方法一:API调用 //加入引用 using System.Runtime.InteropServices;[DllImport("user32.dll")]public static extern bool ReleaseCapture();[DllImport("user32.dll")]public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wPa…

【从零开始学习 SystemVerilog】11.4、SystemVerilog 断言—— $rose, $fell, $stable

序列(sequence)是SystemVerilog断言中的一个简单构建块,可以表示某些表达式,以帮助创建更复杂的属性。 文章目录 Simple Sequence$rose$fell$stableSimple Sequence module tb;bit a;bit clk;// This sequence states that a should be high on every posedge clksequence…

【从零开始学习 SystemVerilog】11.5、SystemVerilog 断言—— Assertion Time delay ##

到目前为止,在以前的文章中,在每个时钟边缘都检查简单的布尔表达式。但顺序检查需要几个时钟周期才能完成,时间延迟由##符号指定。 ## Operator 如果a在任何给定时钟周期上不是高,则序列在同一周期上开始并失败。然而,如果a在任何时钟上为高,则断言开始,如果b在2个时钟…