python requests抓取one推送文字和图片代码实例
作者:changfan 时间:2023-10-26 23:11:16
这篇文章主要介绍了python requests抓取one推送文字和图片代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
requests是Python中一个第三方库,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库。它比 urllib 更加方便,可以节约我们大量的工作,完全满足 HTTP 测试需求。接下来将记录一下requests的使用:
from bs4 import BeautifulSoup
from lxml import html
import xml
import requests
#下载图片函数
def download_img(url,name):
""""
下载指定url的图片
url:图片的url;
name:保存图片的名字
"""
try:
respone = requests.get(url)
f_img = respone.content
path = r'C:\Users\86131\Desktop\itchat\send_file\images\\%s.jpg'%(name)
with open(path, "wb")as f:
f.write(f_img)
except Exception as e:
print("---------地址出错------------")
url_list = []
f = requests.get("http://wufazhuce.com/")
# #打印网页内容
# print(f.content.decode())
soup = BeautifulSoup(f.content,"lxml")
try:
first_div = soup.find("div",attrs={'id':'main-container'}).find('div',attrs={'class':'carousel-inner'})
a_all = first_div.find_all('a')
for i in a_all:
url_list.append(i.attrs['href'])
except Exception as e:
print("---------出错------------")
#得到one的首页推荐页面
f_1 = requests.get(url_list[0])
#打印网页内容
# print(f_1.content.decode())
soup_1 = BeautifulSoup(f_1.content,"lxml")
try:
second_div = soup_1.find("div",attrs={'id':'main-container'}).find('div',attrs={'class':'one-cita-wrapper'})
third_div = soup_1.find("div",attrs={'id':'main-container'}).find('div',attrs={'class':'one-imagen'})
#获得时期值
now_month = second_div.find('p',attrs={'class':'may'}).text
now_one_day = second_div.find('p',attrs={'class':'dom'}).text
#获得图片的url
img_url = third_div.find('img').attrs['src']
#获得一段话并去除开头的空格
one_text = second_div.find("div",attrs={'class':'one-cita'}).text.strip()
#将获得日期拼接
now_day = now_one_day +' '+ now_month
#调用函数下载图片
download_img(img_url, now_day)
except Exception as e:
print("---------出错------------")
来源:https://www.cnblogs.com/changfan/p/11113471.html
标签:python,requests,抓取
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python format 格式化输出方法
2023-12-24 16:26:19
Python数据可视化之环形图
2022-03-26 04:24:12
![](https://img.aspxhome.com/file/2023/5/76905_0s.png)
JS/jQ实现免费获取手机验证码倒计时效果
2023-09-23 05:21:41
![](https://img.aspxhome.com/file/2023/7/56287_0s.jpg)
详解Python中for循环的使用
2023-06-13 13:55:40
解决 myJSFrame 框架中 Ajax 方法一处明显的内存泄露
2008-03-09 19:14:00
用JAVASCRIPT格式化数字成货币(逗号隔开)
2008-01-30 12:34:00
Golang定时器Timer与Ticker的使用详解
2024-04-27 15:27:44
Python3.8.2安装包及安装教程图文详解(附安装包)
2021-11-17 19:35:14
![](https://img.aspxhome.com/file/2023/1/68481_0s.jpg)
python等待10秒执行下一命令的方法
2022-02-25 00:40:03
小 200 行 Python 代码制作一个换脸程序
2021-02-12 09:47:52
![](https://img.aspxhome.com/file/2023/1/124101_0s.jpg)
解决mybatis使用char类型字段查询oracle数据库时结果返回null问题
2024-01-26 03:05:05
Pandas数据结构中Series属性详解
2021-12-13 22:32:17
基于python和flask实现http接口过程解析
2022-06-01 11:46:36
javascript实现右下角广告框效果
2024-04-17 10:25:08
![](https://img.aspxhome.com/file/2023/5/136275_0s.jpg)
MooTools的Cookie类太“自作聪明”了
2008-11-13 12:59:00
Pandas中resample方法详解
2021-10-03 09:57:40
Win10下用Anaconda安装TensorFlow(图文教程)
2021-10-31 09:11:43
![](https://img.aspxhome.com/file/2023/1/110031_0s.jpg)
JS高阶函数原理与用法实例分析
2024-05-09 10:36:21
Python入门教程(三十九)Python的NumPy安装与入门
2023-08-25 10:38:30
![](https://img.aspxhome.com/file/2023/2/82822_0s.png)
Pycharm Available Package无法显示/安装包的问题Error Loading Package List解决
2022-09-09 18:33:23
![](https://img.aspxhome.com/file/2023/7/135147_0s.png)