网络编程
位置:首页>> 网络编程>> Python编程>> Python爬虫爬取一个网页上的图片地址实例代码

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

作者:powerpoint_2016  发布时间:2021-07-25 09:58:25 

标签: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爬虫爬取一个网页上的图片地址实例代码

来源:http://blog.csdn.net/m0_38066258/article/details/77388350

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com