Python 爬虫爬取多页数据

1.获取请求信息,如下图所示,控制台选择Network->XHR,此时,点击页面跳转按钮,控制台会出现发出的请求,然后选择发出请求的文件(第三步),然后选择Headers,下方显示的就是请求头文件信息。

2,使用Python 模拟请求,在Headers下找到 Request Headers 部分,这是请求的头数据。

然后找到Form Data

复制以上内容,形成如下代码


`headers = {     'Accept': 'text/html, */*; q=0.01',     'Accept-Encoding': 'gzip, deflate',     'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,ko;q=0.7',          'Connection': 'keep-alive',     'Content-Length': '61',     'Cookie': 'route=bd118df546101f9fcee5c1a58356a008; JSESSIONID=047BD79E9754BAED525EFE860760393E',     'Host': 'www.chinaooc.cn',     'Origin': 'http://www.chinaooc.cn',     'Pragma': 'no-cache',     'Referer': 'http://www.chinaooc.cn/front/show_index.htm',     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36',          'X-Requested-With': 'XMLHttpRequest',     'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8'     }       form_data = {     'pager.pageNumber':'2',     'pager.pageSize': '50',     'pager.keyword': '',     'mode': 'page'     }`



模拟发送请求,每次改变form_data中的页码就能获得不同的数据,代码如下:


`form_data['pager.pageNumber']=times url = 'http://www.chinaooc.cn/front/show_index.htm' response = requests.post(url, data=form_data, headers=headers)`



3,分析response中返回的信息即可获得数据。

完整代码如下:


`#!/usr/bin/env python  # -*- coding: utf-8 -*- import requests import re from bs4 import BeautifulSoup  class item:     def __init__(self):         self.num=0         self.school=''         self.clazz=''         self.url=''            headers = {     'Accept': 'text/html, */*; q=0.01',     'Accept-Encoding': 'gzip, deflate',     'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,ko;q=0.7',          'Connection': 'keep-alive',     'Content-Length': '61',     'Cookie': 'route=bd118df546101f9fcee5c1a58356a008; JSESSIONID=047BD79E9754BAED525EFE860760393E',     'Host': 'www.chinaooc.cn',     'Origin': 'http://www.chinaooc.cn',     'Pragma': 'no-cache',     'Referer': 'http://www.chinaooc.cn/front/show_index.htm',     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36',          'X-Requested-With': 'XMLHttpRequest',     'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8'     }       form_data = {     'pager.pageNumber':'2',     'pager.pageSize': '50',     'pager.keyword': '',     'mode': 'page'     } times =20 while times < 34:          form_data['pager.pageNumber']=times     url = 'http://www.chinaooc.cn/front/show_index.htm'     response = requests.post(url, data=form_data, headers=headers)       soup = BeautifulSoup(response.content, "html.parser")      tr_list = soup.find_all('tr')     my_tr_list = tr_list[1:-1]      for tr in my_tr_list:         td_list = tr.find_all('td')                  a = item()         a.num = td_list[0].contents[0]         a.school = td_list[1].contents[0]         a.clazz = td_list[2].contents[0].replace('\"',' ')         a.url = td_list[5].find_all('a')[0]["href"]         #name =          with open('E:/data/'+'['+a.num+']['+a.school+']['+a.clazz+'].html','wb') as f:             res = requests.get(a.url)             res.encoding = res.apparent_encoding             f.write(res.content)     times= times+1`

    

    

    

    



我的个人博客 http://weidawang.xyz

最后

小编综合了阿里的面试题做了一份前端面试题PDF文档,里面有面试题的详细解析

虽只说了一个公司的面试,但我们可以知道大厂关注的东西并举一反三,通过一个知识点延伸到另一个知识点,这是我们要掌握的学习方法,小伙伴们在这篇有学到的请评论点赞转发告诉小编哦,谢谢大家的支持!

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值