Python爬虫爬取一个网页上的图片地址实例代码

yipeiwu_com7年前Python爬虫

本文实例主要是实现爬取一个网页上的图片地址,具体如下。

读取一个网页的源代码:

import urllib.request
def getHtml(url):
  html=urllib.request.urlopen(url).read()
  return html
print(getHtml(http://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word=%E5%A3%81%E7%BA%B8&ct=201326592&lm=-1&v=flip))

利用正则表达式爬取一个网页上的图片地址:

import re
import urllib.request
def getHtml(url):
  html=urllib.request.urlopen(url).read()
  return html
def getImg(html):
  r=r'"thumbURL":"(http://img.+?\.jpg)"' #定义正则
  imglist=re.findall(r,html)
  return imglist
html=str(getHtml("http://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word=%E5%A3%81%E7%BA%B8&ct=201326592&lm=-1&v=flip"))
print(getImg(html))

运行结果:

总结

以上就是本文关于Python爬虫爬取一个网页上的图片地址实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

相关文章

python网络爬虫采集联想词示例

python爬虫_采集联想词代码 复制代码 代码如下:#coding:utf-8import urllib2import urllibimport reimport timefrom r...

python urllib爬虫模块使用解析

前言 网络爬虫也称为网络蜘蛛、网络机器人,抓取网络的数据。其实就是用Python程序模仿人点击浏览器并访问网站,而且模仿的越逼真越好。一般爬取数据的目的主要是用来做数据分析,或者公司项目...

利用PyCharm Profile分析异步爬虫效率详解

利用PyCharm Profile分析异步爬虫效率详解

今天比较忙,水一下 下面的代码来源于这个视频里面提到的,github 的链接为:github.com/mikeckenned…(本地下载) 第一个代码如下,就是一个普通的 for 循环...

Python进阶之使用selenium爬取淘宝商品信息功能示例

本文实例讲述了Python进阶之使用selenium爬取淘宝商品信息功能。分享给大家供大家参考,具体如下: # encoding=utf-8 __author__ = 'Jonny'...

使用python BeautifulSoup库抓取58手机维修信息

直接上代码: 复制代码 代码如下:#!/usr/bin/python# -*- coding: utf-8 -*- import urllib import os,datetime,st...