python3 爬虫实战之爬取网易新闻APP端

(一)使用工具

这里使用了火狐浏览器的user-agent插件,不懂的可以点这里火狐插件使用

image.png

(二)爬虫操作步骤:

百度 网易新闻并选择

步骤一:

image.png

步骤二:

image.png

步骤三:

image.png

步骤四:

image.png

最后一步:

image.png

注意点:

(1)网易新闻类型,一共是下面的几种:

{"BBM54PGAwangning","BCR1UC1Qwangning","BD29LPUBwangning","BD29MJTVwangning","C275ML7Gwangning"}

(2)新闻翻页动作:
从0-10 ------> 10-10
步数为10,0起步

我们试着从0页开始获取一下(浏览器输入一下):

http://3g.163.com/touch/reconstruct/article/list/BD29LPUBwangning/0-10.html

image.png

很好的json,有没有。下面开始coding,不多说,直接看代码。

(三) 代码编写部分:

使用环境:win10 python3 scrapy

这里给出了spider文件部分

# -*- coding: utf-8 -*-
# @Time    : 2018/5/23 13:56
# @Author  : 蛇崽
# @Email   : 643435675@QQ.com
# @File    : wangyi3g.py
import json

import re
import scrapy
from bs4 import BeautifulSoup


class Wangyi3GSpider(scrapy.Spider):

    name = 'wangyi3g'
    allowed_domains = ['3g.163.com']
    start_urls = ['http://3g.163.com/touch/news/']

    baseurl = 'http://3g.163.com/touch/reconstruct/article/list/BD29LPUBwangning/{}-10.html'
    def parse(self, response):
        # 10 20
        for page in range(0,80,10):
            jsonurl = self.baseurl.format(page)
            yield scrapy.Request(jsonurl,callback=self.parse_li_json)

    def parse_li_json(self,response):
        res = response.body.decode('utf-8')
        print(res)
        res = str(res).replace('artiList(','')
        res = res.replace(')','')
        j = json.loads(res)
        datas = j['BD29LPUBwangning']
        print(datas)

        for data in datas:
            title = data['title']
            ptime = data['ptime']
            url = data['url']
            source = data['source']
            print(title,ptime,url,source)
            if url:
                yield scrapy.Request(url,callback=self.parse_detail)

    def parse_detail(self,response):
        soup = BeautifulSoup(response.body,'lxml')
        content = soup.find('div','content')
        image_urls = re.findall(r'data-src="(.*?)"', str(content))
        # print(image_urls)

image.png

以上就是网易新闻APP爬虫代码的实现,更多技术学习交流可查看主页加群。我们一起学习。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,676评论 25 709
  • 上一周的今天我还特别特别难过,因为感冒发烧过敏眼睛脖子脸都肿了,随时担心自己死在这个小村里,还不敢和家人朋友说,默...
    亦北阅读 208评论 0 0
  • 一开始你是我的秘密。我怕你知道,又怕你不知道,又怕你装作不知道。你不说,我不说,又远又近。 喜欢仿佛是一个人与生俱...
    糖甜甜sweet阅读 185评论 0 0
  • 原本只是去折了支花, 闻着闻着就决定, 把今秋留下。 明年今天她还会开吧, 明年今天我还会在树下吗? 有花堪折直须...
    晶童阅读 242评论 1 4