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 字符串转换为整形和浮点类型的方法
Jul 17 Python
Python使用装饰器模拟用户登陆验证功能示例
Aug 24 Python
django解决跨域请求的问题
Nov 11 Python
解决Python中定时任务线程无法自动退出的问题
Feb 18 Python
对python特殊函数 __call__()的使用详解
Jul 02 Python
python3的print()函数的用法图文讲解
Jul 16 Python
python3反转字符串的3种方法(小结)
Nov 07 Python
Python破解BiliBili滑块验证码的思路详解(完美避开人机识别)
Feb 17 Python
Django 构建模板form表单的两种方法
Jun 14 Python
python如何使用代码运行助手
Jul 03 Python
Python3 多线程(连接池)操作MySQL插入数据
Jun 09 Python
python 离散点图画法的实现
Apr 01 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中使用CURL伪造来路抓取页面或文件
2011/05/04 PHP
php使用cookie实现记住用户名和密码实现代码
2015/04/27 PHP
Laravel学习教程之View模块详解
2017/09/18 PHP
php与阿里云短信接口接入操作案例分析
2020/05/27 PHP
JavaScript高级程序设计
2006/12/29 Javascript
$.get获取一个文件的内容示例代码
2013/09/11 Javascript
jquery插件jTimer(jquery定时器)使用方法
2013/12/23 Javascript
JS中类或对象的定义说明
2014/03/10 Javascript
AngularJS中的包含详细介绍及实现示例
2016/07/28 Javascript
Bootstrap弹出框modal上层的输入框不能获得焦点问题的解决方法
2016/12/13 Javascript
jQuery实现限制文本框的输入长度
2017/01/11 Javascript
CheckBox多选取值及判断CheckBox选中是否为空的实例
2017/10/31 Javascript
结合mint-ui移动端下拉加载实践方法总结
2017/11/08 Javascript
vue-cli2打包前和打包后的css前缀不一致的问题解决
2018/08/24 Javascript
Nodejs把接收图片base64格式保存为文件存储到服务器上
2018/09/26 NodeJs
VUE接入腾讯验证码功能(滑块验证)备忘
2019/05/07 Javascript
JS removeAttribute()方法实现删除元素的某个属性
2021/01/11 Javascript
[01:24:51]2014 DOTA2华西杯精英邀请赛 5 25 LGD VS NewBee第二场
2014/05/26 DOTA
[03:11]TI9战队档案 - Alliance
2019/08/20 DOTA
python检测是文件还是目录的方法
2015/07/03 Python
Python 操作文件的基本方法总结
2017/08/10 Python
Python文本特征抽取与向量化算法学习
2017/12/22 Python
django表单实现下拉框的示例讲解
2018/05/29 Python
浅谈Python 多进程默认不能共享全局变量的问题
2019/01/11 Python
Python中logging日志库实例详解
2020/02/19 Python
python开发实例之Python的Twisted框架中Deferred对象的详细用法与实例
2020/03/19 Python
关于python3.7安装matplotlib始终无法成功的问题的解决
2020/07/28 Python
html5+css3之制作header实例与更新
2020/12/21 HTML / CSS
SQL Server 2000数据库的文件有哪些,分别进行描述。
2015/11/09 面试题
制衣厂各岗位职责
2013/12/02 职场文书
综艺节目策划方案
2014/06/13 职场文书
社区志愿者活动方案
2014/08/18 职场文书
《领导干部从政道德启示录》学习心得体会
2016/01/20 职场文书
人生感悟经典句子
2019/08/20 职场文书
Python使用openpyxl模块处理Excel文件
2022/06/05 Python
Win11怎么添加用户?Win11添加用户账户的方法
2022/07/15 数码科技