python爬虫实现获取下一页代码


Posted in Python onMarch 13, 2020

我们首先来看下实例代码:

from time import sleep

import faker
import requests
from lxml import etree

fake = faker.Faker()

base_url = "http://angelimg.spbeen.com"

def get_next_link(url):
  content = downloadHtml(url)
  html = etree.HTML(content)
  next_url = html.xpath("//a[@class='ch next']/@href")
  if next_url:
    return base_url + next_url[0]
  else:
    return False

def downloadHtml(ur):
  user_agent = fake.user_agent()
  headers = {'User-Agent': user_agent,"Referer":"http://angelimg.spbeen.com/"}
  response = requests.get(url, headers=headers)
  return response.text

def getImgUrl(content):
  html = etree.HTML(content)
  img_url = html.xpath('//*[@id="content"]/a/img/@src')
  title = html.xpath(".//div['@class=article']/h2/text()")

  return img_url[0],title[0]

def saveImg(title,img_url):
  if img_url is not None and title is not None:
    with open("txt/"+str(title)+".jpg",'wb') as f:
      user_agent = fake.user_agent()
      headers = {'User-Agent': user_agent,"Referer":"http://angelimg.spbeen.com/"}
      content = requests.get(img_url, headers=headers)
      #request_view(content)
      f.write(content.content)
      f.close()

def request_view(response):
  import webbrowser
  request_url = response.url
  base_url = '<head><base href="%s" rel="external nofollow" >' %(request_url)
  base_url = base_url.encode()
  content = response.content.replace(b"<head>",base_url)
  tem_html = open('tmp.html','wb')
  tem_html.write(content)
  tem_html.close()
  webbrowser.open_new_tab('tmp.html')

def crawl_img(url):
  content = downloadHtml(url)
  res = getImgUrl(content)
  title = res[1]
  img_url = res[0]
  saveImg(title,img_url)

if __name__ == "__main__":
  url = "http://angelimg.spbeen.com/ang/4968/1"

  while url:
    print(url)
    crawl_img(url)
    url = get_next_link(url)

python 爬虫如何执行自动下一页循环加载文字

from bs4 import BeautifulSoup
import requests
import time
from lxml import etree
import os
# 该demo执行的为如何利用bs去爬一些文字
def start():
  # 发起网络请求
  html=requests.get('http://www.baidu.com')
  #编码
  html.encoding=html.apparent_encoding
  #创建sp
  soup=BeautifulSoup(html.text,'html.parser')
  print(type(soup))
  print('打印元素')
  print(soup.prettify())
  #存储一下title 该方法没有提示直接展示
  title=soup.head.title.string
  print(title)
#   写入文本
  with open(r'C:/Users/a/Desktop/a.txt','w') as f:
    f.write(title)
  print(time.localtime())
 
url_2 = 'http://news.gdzjdaily.com.cn/zjxw/politics/sz_4.shtml'
def get_html_from_bs4(url):
 
  # response = requests.get(url,headers=data,proxies=ip).content.decode('utf-8')
  response = requests.get(url).content.decode('utf-8')
  soup = BeautifulSoup(response, 'html.parser')
  next_page = soup.select('#displaypagenum a:nth-of-type(9)')[0].get('href')
  # for i in nett
  print(next_page)
  next2='http://news.gdzjdaily.com.cn/zjxw/politics/'+next_page
 
 
def get_html_from_etree(url):
 
  response = requests.get(url).content.decode('utf-8')
  html= etree.HTML(response)
 
  next_page = html.xpath('.//a[@class="PageNum"][8]/@href')[0]
  print(next_page)
  # next2='http://news.gdzjdaily.com.cn/zjxw/politics/'+next_page
 
 
get_html_from_etree(url_2)
 
if __name__ == '__main__':
  start()

到此这篇关于python爬虫实现获取下一页代码的文章就介绍到这了,更多相关python爬虫获取下一页内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
python文件读写并使用mysql批量插入示例分享(python操作mysql)
Feb 17 Python
python中pycurl库的用法实例
Sep 30 Python
Python中使用logging模块打印log日志详解
Apr 05 Python
在Python的Flask框架中验证注册用户的Email的方法
Sep 02 Python
详解Python中的相对导入和绝对导入
Jan 06 Python
不同版本中Python matplotlib.pyplot.draw()界面绘制异常问题的解决
Sep 24 Python
Python向MySQL批量插数据的实例讲解
Mar 31 Python
python实现合并多个list及合并多个django QuerySet的方法示例
Jun 11 Python
使用python实现对元素的长截图功能
Nov 14 Python
Python time库基本使用方法分析
Dec 13 Python
python re模块匹配贪婪和非贪婪模式详解
Feb 11 Python
Pytest参数化parametrize使用代码实例
Feb 22 Python
Python3 利用face_recognition实现人脸识别的方法
Mar 13 #Python
在django中使用post方法时,需要增加csrftoken的例子
Mar 13 #Python
python 安装教程之Pycharm安装及配置字体主题,换行,自动更新
Mar 13 #Python
详解用Python进行时间序列预测的7种方法
Mar 13 #Python
django-xadmin根据当前登录用户动态设置表单字段默认值方式
Mar 13 #Python
在django项目中导出数据到excel文件并实现下载的功能
Mar 13 #Python
Django choices下拉列表绑定实例
Mar 13 #Python
You might like
给php新手谈谈我的学习心得
2007/02/25 PHP
php的memcached客户端memcached
2011/06/14 PHP
php实现邮件发送并带有附件
2014/01/24 PHP
php如何连接sql server
2015/10/16 PHP
php用xpath解析html的代码实例讲解
2019/02/14 PHP
javascript学习网址备忘
2007/05/29 Javascript
js下通过prototype扩展实现indexOf的代码
2010/12/08 Javascript
jquery中通过父级查找进行定位示例
2013/06/28 Javascript
超链接的禁用属性Disabled使用示例
2014/07/31 Javascript
纯JavaScript实现获取onclick、onchange等事件的值
2014/12/29 Javascript
javascript实现左右控制无缝滚动
2014/12/31 Javascript
Javascript实现Web颜色值转换
2015/02/05 Javascript
JS实现简洁、全兼容的拖动层实例
2015/05/13 Javascript
基于JavaScript实现购物网站商品放大镜效果
2016/09/06 Javascript
Bootstrap基本样式学习笔记之图片(6)
2016/12/07 Javascript
js 概率计算(简单版)
2017/09/12 Javascript
js中apply与call简单用法详解
2017/11/06 Javascript
Layui事件监听的实现(表单和数据表格)
2019/10/17 Javascript
js实现图片无缝循环轮播
2019/10/28 Javascript
Vue实现星级评价效果实例详解
2019/12/30 Javascript
[46:53]Secret vs Liquid 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/17 DOTA
python通过pil模块获得图片exif信息的方法
2015/03/16 Python
Python数据结构与算法之图的最短路径(Dijkstra算法)完整实例
2017/12/12 Python
python实现决策树分类算法
2017/12/21 Python
python dataframe向下向上填充,fillna和ffill的方法
2018/11/28 Python
pytorch1.0中torch.nn.Conv2d用法详解
2020/01/10 Python
英国书籍、CD、DVD和游戏的第一道德零售商:Awesome Books
2020/02/22 全球购物
公务员培训自我鉴定
2013/09/19 职场文书
医学院学生的自我评价分享
2013/11/19 职场文书
小学毕业感言50字
2014/02/16 职场文书
浪漫婚礼主持词
2014/03/14 职场文书
洗手间标语
2014/06/23 职场文书
如何写观后感
2015/06/19 职场文书
如何书写邀请函?
2019/06/24 职场文书
python非标准时间的转换
2021/07/25 Python
Python人工智能之混合高斯模型运动目标检测详解分析
2021/11/07 Python