zoukankan      html  css  js  c++  java
  • 学习强国网页爬取)

    需求

    https://www.xuexi.cn/f997e76a890b0e5a053c57b19f468436/018d244441062d8916dd472a4c6a0a0b.html页面中的新闻数据。

    项目分析

    1 首先我们通过请求网页地址响应数据中查看浏览器页面的数据是否存在于网页html中.

    2  在网页响应的html 文件中不存在我们页面数据,因此学习强国网的新闻数据都是动态加载出来的,并且通过抓包工具,发现也不是ajax请求(因为没有捕获ajax请求的数据包),那这里的数据只有可能是通过js生成的.

     

    3 通过谷歌浏览器自带的抓包工具,我们查看是哪一个js请求的数据格式.打开开发者应用,刷新页面.

      4 查看数据响应的详细信息

     5 同样可以拿到详情页的url

     

    6 url分析

    详情    页面:https://www.xuexi.cn/e5577906b82bc00b102d2c8d3b723312/e43e220633a65f9b6d8b53712cba9caa.html

    详情页数据:https://www.xuexi.cn/e5577906b82bc00b102d2c8d3b723312/datae43e220633a65f9b6d8b53712cba9caa.js

    全栈爬取代码实现

    import requests
    import re
    import json
    from lxml import etree
    
    class Spider:
        def __init__(self, headers, url,fp=None):
            self.headers = headers
            self.url=url
            self.fp=fp
    
        def open_file(self):
            self.fp = open('学习强国2.txt', 'w', encoding='utf8')
    
        def get_data(self):
           return requests.get(url=self.url, headers=self.headers).text
    
        def parse_home_data(self):
            ex = '"static_page_url":"(.*?)"'
            home_data=self.get_data()
            return re.findall(ex,home_data)
    
        def parse_detail_data(self):
            detail_url=self.parse_home_data()
            print(detail_url)
            i = 0
            for url in detail_url:
                i += 1
                '''<title>系统维护中</title> 坑人'''
                try:
                    self.url = url.replace(r'/e', r'/datae').replace('html', 'js')
                    detail_data = self.get_data()
                    detail_data = detail_data.replace('globalCache = ', '')[:-1]
                    dic_data = json.loads(detail_data)
              #获取字典中的第一个键值对的key first
    = list(dic_data.keys())[0] title = dic_data[first]['detail']['frst_name'] content_html = dic_data[first]['detail']['content_list'][0]['content'] tree = etree.HTML(content_html) content_list = tree.xpath('.//p/text()') except Exception as e: print(e) continue self.fp.write(f'第{i}章' + title + ' ' + ''.join(content_list) + ' ') def close_file(self): self.fp.close() def run(self): self.open_file() self.parse_detail_data() self.close_file() if __name__ == '__main__': headers = { 'Host': 'www.xuexi.cn', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36', } url = 'https://www.xuexi.cn/f997e76a890b0e5a053c57b19f468436/data018d244441062d8916dd472a4c6a0a0b.js' spider=Spider(url=url,headers=headers) spider.run()

    效果:

      一共64篇文章

     

  • 相关阅读:
    2020.10.23 19级training 补题报告
    2020.10.17 天梯赛练习 补题报告
    2020.10.16 19级training 补题报告
    2020.10.9 19级training 补题报告
    2020.10.10 天梯赛练习 补题报告
    2020.10.3 天梯赛练习 补题报告
    2020.10.2 19级training 补题报告
    第十届山东省ACM省赛复现补题报告
    VVDI Key Tool Plus Adds VW Passat 2015 Key via OBD
    Xhorse VVDI Prog Software V5.0.3 Adds Many MCUs
  • 原文地址:https://www.cnblogs.com/angle6-liu/p/10580635.html
Copyright © 2011-2022 走看看