python爬虫学习笔记

本文是Python爬虫学习笔记,涵盖爬虫基础知识、模块讲解,包括requests库的使用、循环抓取页面、豆瓣排行榜和百度翻译接口的抓取、人人网代码登录、JSON数据解析、正则表达式、XPath和BeautifulSoup解析网页。还涉及了反爬策略分析和多线程初步探讨。
摘要由CSDN通过智能技术生成

爬虫基础

day01

爬虫学习来源:(逆风学习网:买的 2019年3月份黑马爬虫阶段课程,很遗憾没有文档。。。)(获取方法:自行百度

先给出爬虫的总目录:
在这里插入图片描述

爬虫基础知识

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在自己电脑上设置爬虫的时候,需要注意几个点:

  1. 首先确认电脑是安装了 python2 和 python3 的吗?如果两个都安装了,而且你还想用 pip 来自动安装库,请使用 链接
    在这里插入图片描述
  2. 安装的库需要看自己是否需要了,现在先安装了 repuests库,注意本文章现在只用了 python3 !!! 如果更新了库,使用的软件还是 pycharm,请更新 pycharm 的 python3 的信息,只需要在下图刷新一下就行了。
    在这里插入图片描述

模块一:

在这里插入图片描述
尝试爬取网站信息

如果结果出现乱码,注意设置编码为 UTF-8
代码:

from urllib import request

base_url = 'http://www.baidu.com'

req = request.Request(base_url)
response = request.urlopen(req)

html = response.read()
print(html)

with open('baidu.html ','wb') as f:
    f.write(html)
    f.close()

爬取结果:
在这里插入图片描述

模块二

在这里插入图片描述
这个是各种请求对象的使用:
代码:

import requests
if __name__ == '__main__':
    url = 'http://www.baidu.com'
    response = requests.get(url)

    data = response.content
    data_str = data.decode('utf-8')

    #状态码
    code = response.status_code
    print(code)
    print(type(code))

    #请求头
    requests_headers = response.request.headers
    print(requests_headers)

    #响应头
    response_headers = response.headers
    print(response_headers)

    #请求 cookies——RequestsCookieJar 对象 有时是 _cookies
    request_cookies = response.request._cookies
    print(request_cookies)

    #响应的 cookie
    response_cookies = response.cookies
    print(response_cookies)

    #保存文件
    # with open('02baidu.html','w') as f:
    #     f.write(data_str)
    print('结束')

结果:
在这里插入图片描述

模块三 循环抓取页面

import requests


class TieBaSpider(object):
    def __init__(self):
        self.tiebaName = input('输入贴吧名字:')
        self.startPage = int(input('开始页数:'))
        self.endPage = int(input('结束页数:'))


        self.base_url = 'http://tieba.baidu.com/f'
        self.headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24'}

    # 1.发请求
    def send_request(self,tieba_params):
        response = requests.get(url=self.base_url, headers=self.headers, params=tieba_params)
        data = response.content
        print(555)
        return data

    # 2.保存数据
    def write_file(self, data, page):
        file_pat = 'TieBa/' + str(page) + '.html'
        print('正在抓取{}页...'.format(page))
        with open(file_pat, 'wb') as f:
            print('666')
            f.write(data)

    # 3.调度方法
    def run(self):
        for page in range(self.startPage, self.endPage + 1):
            # 1.拼接参数
            tieba_params = {
                'kw': self.tiebaName,
                'pn': (page - 1) * 50
            }
            # 2.发请求
            data = self.send_request(tieba_params)
           # 3.保存数据
            self.write_file(data, page)


tool = TieBaSpider()
tool.run()

到这里就算是爬虫的基本应用了。

在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值