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使用webbrowser浏览指定url的方法
Apr 04 Python
python类和函数中使用静态变量的方法
May 09 Python
Python爬虫爬取一个网页上的图片地址实例代码
Jan 16 Python
分享一下Python数据分析常用的8款工具
Apr 29 Python
python实现逐个读取txt字符并修改
Dec 24 Python
python实现简单图片物体标注工具
Mar 18 Python
Python入门Anaconda和Pycharm的安装和配置详解
Jul 16 Python
python爬虫 execjs安装配置及使用
Jul 30 Python
详细整理python 字符串(str)与列表(list)以及数组(array)之间的转换方法
Aug 30 Python
Python面向对象原理与基础语法详解
Jan 02 Python
浅谈Python爬虫原理与数据抓取
Jul 21 Python
Python语言规范之Pylint的详细用法
Jun 24 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文件缓存内容保存格式实例分析
2014/08/20 PHP
php时间函数用法分析
2016/05/28 PHP
PHP5.5新特性之yield理解与用法实例分析
2019/01/11 PHP
PHP中散列密码的安全性分析
2019/07/26 PHP
兼容ie、firefox的图片自动缩放的css跟js代码分享
2012/01/21 Javascript
jQuery数组处理代码详解(含实例演示)
2012/02/03 Javascript
httpclient模拟登陆具体实现(使用js设置cookie)
2013/12/11 Javascript
JavaScript使用encodeURI()和decodeURI()获取字符串值的方法
2015/08/04 Javascript
jquery实现的蓝色二级导航条效果代码
2015/08/24 Javascript
javascript html5移动端轻松实现文件上传
2020/03/27 Javascript
js和jq使用submit方法无法提交表单的快速解决方法
2016/05/17 Javascript
使用jquery获取url以及jquery获取url参数的实现方法
2016/05/25 Javascript
微信小程序 action-sheet底部菜单详解
2016/10/27 Javascript
jQuery选择器之属性过滤选择器详解
2017/09/28 jQuery
JS+WCF实现进度条实时监测数据加载量的方法详解
2017/12/19 Javascript
利用Angular7开发一个Radio组件的全过程
2019/07/11 Javascript
[01:31:03]DOTA2完美盛典全回顾 见证十五项大奖花落谁家
2017/11/28 DOTA
Python random模块常用方法
2014/11/03 Python
Python中为什么要用self探讨
2015/04/14 Python
Python数据结构与算法之图结构(Graph)实例分析
2017/09/05 Python
Python读取图片为16进制表示简单代码
2018/01/19 Python
Python网络编程使用select实现socket全双工异步通信功能示例
2018/04/09 Python
Python二叉树定义与遍历方法实例分析
2018/05/25 Python
Pandas 按索引合并数据集的方法
2018/11/15 Python
python从子线程中获得返回值的方法
2019/01/30 Python
Python实现的多进程拷贝文件并显示百分比功能示例
2019/04/09 Python
Python实现通过解析域名获取ip地址的方法分析
2019/05/17 Python
python shapely.geometry.polygon任意两个四边形的IOU计算实例
2020/04/12 Python
在Ubuntu中安装并配置Pycharm教程的实现方法
2021/01/06 Python
ALEX AND ANI:手镯,项链,耳环和更多
2017/04/20 全球购物
菲律宾领先的在线时尚商店:Zalora菲律宾
2018/02/08 全球购物
美国健康和保健平台:healtop
2020/07/02 全球购物
Java里面如何把一个Array数组转换成Collection, List
2013/07/26 面试题
求职信模板怎么做
2014/01/26 职场文书
有限责任公司股东合作协议书
2014/12/02 职场文书
JS前端可扩展的低代码UI框架Sunmao使用详解
2022/07/23 Javascript