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 相关文章推荐
linux系统使用python获取cpu信息脚本分享
Jan 15 Python
Python读写Excel文件方法介绍
Nov 22 Python
python Socket之客户端和服务端握手详解
Sep 18 Python
使用Django和Python创建Json response的方法
Mar 26 Python
pyqt5的QWebEngineView 使用模板的方法
Aug 18 Python
python实现控制台打印的方法
Jan 12 Python
python抓取需要扫微信登陆页面
Apr 29 Python
python读写csv文件并增加行列的实例代码
Aug 01 Python
python求解汉诺塔游戏
Jul 09 Python
Selenium python时间控件输入问题解决方案
Jul 22 Python
Django中的JWT身份验证的实现
May 07 Python
OpenCV-Python实现油画效果的实例
Jun 08 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
实现动画效果核心方式的js代码
2013/09/27 Javascript
分享js粘帖屏幕截图到web页面插件screenshot-paste
2020/08/21 Javascript
js下载文件并修改文件名
2017/05/08 Javascript
详解webpack2+React 实例demo
2017/09/11 Javascript
基于vue-cli vue-router搭建底部导航栏移动前端项目
2018/02/28 Javascript
Vue.js 实现微信公众号菜单编辑器功能(一)
2018/05/08 Javascript
原生JS实现的碰撞检测功能示例
2018/05/18 Javascript
详解基于DllPlugin和DllReferencePlugin的webpack构建优化
2018/06/28 Javascript
JS实现点击按钮可实现编辑功能
2018/07/03 Javascript
JavaScript事件冒泡与事件捕获实例分析
2018/08/01 Javascript
利用d3.js制作连线动画图与编辑器的方法实例
2019/09/05 Javascript
LayUI数据接口返回实体封装的例子
2019/09/12 Javascript
js实现GIF图片的分解和合成
2019/10/24 Javascript
[06:45]DOTA2卡尔工作室 英雄介绍幻影长矛手篇
2013/07/12 DOTA
利用Python抓取行政区划码的方法
2016/11/28 Python
python处理Excel xlrd的简单使用
2017/09/12 Python
浅谈python数据类型及类型转换
2017/12/18 Python
pandas带有重复索引操作方法
2018/06/08 Python
利用Python对文件夹下图片数据进行批量改名的代码实例
2019/02/21 Python
用python写一个定时提醒程序的实现代码
2019/07/22 Python
python聚类算法解决方案(rest接口/mpp数据库/json数据/下载图片及数据)
2019/08/28 Python
PyQt5+Caffe+Opencv搭建人脸识别登录界面
2019/08/28 Python
通过celery异步处理一个查询任务的完整代码
2019/11/19 Python
基于python实现百度语音识别和图灵对话
2020/11/02 Python
美国创意之家:BulbHead
2017/07/12 全球购物
最新计算机专业自荐信
2013/10/16 职场文书
车间调度岗位职责
2013/11/30 职场文书
幼儿园元旦家长感言
2014/02/27 职场文书
法人授权委托书
2014/09/16 职场文书
“六查”、“三学”、“三干”查摆问题整改措施
2014/09/27 职场文书
党员批评与自我批评发言稿
2014/10/14 职场文书
高三毕业感言
2015/07/30 职场文书
导游词之蓬莱长岛
2019/12/17 职场文书
Pyqt5将多个类组合在一个界面显示的完整示例
2021/09/04 Python
Win11如何启用启动修复 ? Win11执行启动修复的三种方法
2022/04/08 数码科技
SQL Server中的逻辑函数介绍
2022/05/25 SQL Server