python requests抓取one推送文字和图片代码实例

yipeiwu_com6年前Python爬虫

这篇文章主要介绍了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("---------出错------------")

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

详解python爬虫系列之初识爬虫

详解python爬虫系列之初识爬虫

前言 我们这里主要是利用requests模块和bs4模块进行简单的爬虫的讲解,让大家可以对爬虫有了初步的认识,我们通过爬几个简单网站,让大家循序渐进的掌握爬虫的基础知识,做网络爬虫还是需...

一步步教你用python的scrapy编写一个爬虫

一步步教你用python的scrapy编写一个爬虫

介绍 本文将介绍我是如何在python爬虫里面一步一步踩坑,然后慢慢走出来的,期间碰到的所有问题我都会详细说明,让大家以后碰到这些问题时能够快速确定问题的来源,后面的代码只是贴出了核心...

Pyspider中给爬虫伪造随机请求头的实例

Pyspider 中采用了 tornado 库来做 http 请求,在请求过程中可以添加各种参数,例如请求链接超时时间,请求传输数据超时时间,请求头等等,但是根据pyspider的原始框...

python实现知乎高颜值图片爬取

导入相关包 import time import pydash import base64 import requests from lxml import etree from...

Python之Scrapy爬虫框架安装及简单使用详解

Python之Scrapy爬虫框架安装及简单使用详解

题记:早已听闻python爬虫框架的大名。近些天学习了下其中的Scrapy爬虫框架,将自己理解的跟大家分享。有表述不当之处,望大神们斧正。 一、初窥Scrapy Scrapy是一个为了爬...