Python网页信息爬取脚本

文章目录

获取整个页面所有源码

该步骤可以用requests模块实现,分为下面步骤:

定义一个URL 地址;

发送HTTP 请求;

处理HTTP 响应。

下面代码定义了一个get_Html方法,用requests.get()发送get请求,并用res变量接收,然后用.content返回字符串的二进制格式(操作更方便)给get_Html方法,最后调用并把数据给html,并用print输出:

.content返回字符串的二进制格式(操作更方便)

.decode("utf8"),二进制转码成utf8格式(显示中文)

python 复制代码
import requests

url = "http://10.9.75.164/pythonSpider/index.html"

def get_Html(url):
    res = requests.get(url = url)

    return res.content


html=get_Html(url = url)

print(html.decode("utf8"))

如下图,在命令行执行后获取到目标url的网页源码:

筛选出源码中图片地址

查看网页图片的地址,发现它们都是以style/u开头的URL:

这时候可以用正则来匹配图片地址,用正则匹配出的图片地址是一个列表,用for循环遍历输出:

\w*,匹配所有数字和字母

\.转义点,当做普通字符处理

.decode(),将获取到的源码从二进制转为字符串

python 复制代码
import requests
import re

url = "http://10.9.75.164/pythonSpider/index.html"

def get_Html(url):
    res = requests.get(url = url)

    return res.content


html=get_Html(url = url)

img_path_list = re.findall(r"style/\w*\.jpg",html.decode())

for img_path in img_path_list:
	print(img_path)

运行结果如下,成功筛选出源码中的所有图片地址:

将图片下载到本地

img_save_path="./img/1.jpg",指定图片文件保存的目录

with open(img_save_path,"wb") as f,以写方式(二进制)打开文件

f.write(res.content),将接收到的GTTP源码以二进制格式写入f文件

下面定义了一个img_download方法,它将url分割子串并与img_path拼接(img_path是一个图片地址),然后用requests.get方法发送get请求并用res变量接收,然后指定了一个保存图片的路径,最后用write函数将源码的二进制写入该图片中:

python 复制代码
import requests

url = "http://10.9.75.164/pythonSpider/index.html"

img_path="style/u24020836931378817798fm170s6BA8218A7B2128178FA0A49F010080E2w.jpg"

def img_download(img_path):
    img_url=url[0:url.rfind('/')+1]+ img_path
    
    res=requests.get(url= img_url)
    
    img_save_path="./img/1.jpg"
    
    with open(img_save_path,"wb") as f:
    	f.write(res.content)
    	
img_download(img_path)

执行成功后结果如下:

成功爬取图片到本地:

完整脚本

下面是完整脚本,定义了上述的三个方法, get_html方法获取页面源码,get_img_path_list方法筛选出源码中的图片地址并组成一个列表,img_download将文件下载到本地,其中用时间戳命名每个图片文件。

这段代码调用了三个方法,前两个方法的结果给变量html和img_path_list,然后用for循环将列表遍历,每一次遍历都调用img_download方法:

python 复制代码
html = get_html(url = url).decode()

img_path_list = get_img_path_list(html = html)

for img_path in img_path_list:
	print(img_path)
    img_download(img_path = img_path)

完整脚本:

python 复制代码
import requests
import re
import time

url = "http://10.9.75.164/pythonSpider/index.html"

def get_html(url):
    res = requests.get(url = url)
    return res.content

def get_img_path_list(html):
    img_path_list = re.findall(r"style/\w*\.jpg", html)
    return img_path_list

def img_download(img_path):
    img_url=url[0:url.rfind('/')+1]+ img_path
    res=requests.get(url= img_url)
    img_save_path=f"./img/{time.time()}.jpg"
    with open(img_save_path,"wb") as f:
    	f.write(res.content)

html = get_html(url = url).decode()

img_path_list = get_img_path_list(html = html)

for img_path in img_path_list:
	print(img_path)
    img_download(img_path = img_path)

脚本执行成功:

下载图片到本地成功:

相关推荐
卷无止境7 分钟前
智能体开发环境ADE浅析,编程工具的下一次范式跃迁
后端·python
彧azz2 小时前
图的存储结构详解:邻接矩阵的原理、实现与应用
开发语言·数据结构·学习·php
嵌入式学习菌3 小时前
Modbus‑RTU 数据类型分析
开发语言·单片机·bug
matlab代码4 小时前
基于matlab数字图像处理的指纹识别系统【源码68期】
开发语言·matlab
2601_962300814 小时前
机器学习贴士:使用Python编写MapReduce
hadoop·python·机器学习·mapreduce·数据处理
matlab代码4 小时前
基于matlab的水果识别系统(苹果香蕉菠萝梨桃子)【源码65期】
开发语言·matlab
xyz_CDragon4 小时前
GitHub上4个爆款AI开源Skill:拍照后不用P图,用Codex一键生成高级海报(附效果图+使用教程)
人工智能·python·github·codex·skill
wuyk5554 小时前
从零吃透 MQTT 通信|第 8 章 FreeRTOS 多任务架构下 MQTT 工程架构,任务拆分、队列解耦、临界区保护
c语言·开发语言·stm32·学习·架构
weixin199701080165 小时前
[特殊字符]《跨境二手ERP对接Back Market:标准化API + 7~10工作日技术合规审核实录》(附Python源码)
开发语言·python·pandas
尘客-追梦5 小时前
Day 01:插件化之前,先看清 ABI 这堵墙
开发语言·c++·qt