浅谈Python爬虫(十)【企查查爬虫无需登录】

想起来有次面试的时候,面试官让我爬企查查,当时一脸懵。。。今天正好有时间,索性看一下。

进入企查查,不登录,可以搜索到结果,但是只能看到前5个。。。凑合着也行吧
直接怼一下试试。。。

import requests

headers = {'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36'}
url = 'https://www.qcc.com/web/search?key=腾讯'
response = requests.get(url, headers=headers)
response.encoding = response.apparent_encoding
print(response.text)
print(response)

返回值是200,但是内容是报错。
在这里插入图片描述
在浏览器新开一个无痕页面(Chrome Ctrl+Shift+N),然后把链接复制进去。看一下具体请求。
在这里插入图片描述
发现有一个set-cookie。把这个参数当成cookie放进请求头再请求一下试试看。

import requests

headers = {
    'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36',
    'cookie': 'acw_tc=7ceef52516207237911506404e7bf007f5a106d051edd92806324a8915'
}
url = 'https://www.qcc.com/web/search?key=腾讯'
response = requests.get(url, headers=headers)
response.encoding = response.apparent_encoding
print(response.text)
print(response)

果然可以了。
接下来就是需要找这个值的存放位置了。
找了一下第一次报错返回的源码,发现没有。。。
这时候,突然发现第一次返回的源码是405.html这个文件的内容。那么。。。。这个小可爱重定向了???
换代码试一下

import requests

headers = {'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36'}
url = 'https://www.qcc.com/web/search?key=腾讯'
response = requests.get(url, headers=headers, allow_redirects=False)
response.encoding = response.apparent_encoding
print(response.text)

然后看一下这个返回值,果然有一个类似cookie 的
在这里插入图片描述
虽然不是完全一样,但是这个好像是cookie前面的一部分。
emmm,不管了直接复制进去试一下

import requests

headers = {
    'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36',
    'cookie': 'acw_tc=7ceef51716207240819688967e'
}
url = 'https://www.qcc.com/web/search?key=腾讯'
response = requests.get(url, headers=headers)
response.encoding = response.apparent_encoding
print(response.text)

发现也是可以的啊哈哈哈

所以具体流程就是,先请求一次任意搜索页面,获取到cookie(注意要加上禁止重定向的参数 allow_redirects=False)。然后就可以快乐的抓取了。
剩下就是解析内容页,没有啥技术难度了。
不多BB,直接上代码

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# @File    :   qcc.py    
# @Author  :   Monkey
# @DATE    :   2021/5/11 下午5:13 

import requests
import re
from lxml import etree


class QCC(object):
    """企查查爬虫"""

    def __init__(self):
        self._headers = {
            'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36',
        }

    def get_cookie(self):
        """发起一次测试请求,获取到搜索的cookie"""
        url = 'https://www.qcc.com/web/search?key=测试'
        response = requests.get(url, headers=self._headers, allow_redirects=False)
        response.encoding = 'utf8'
        result = re.findall(r'div>您的请求ID是: <strong>\n(.*?)</strong></div>',  response.text)
        if result:
            return result[0]

    def search(self, search_keyword):
        """搜索"""
        url = 'https://www.qcc.com/web/search?key={}'.format(search_keyword)
        headers = self._headers
        headers['cookie'] = 'acw_tc={}'.format(self.get_cookie())
        response = requests.get(url, headers=headers)
        response.encoding = 'utf8'
        html = etree.HTML(response.text)
        com_url = html.xpath('//a[@class="title"]/@href')
        print('搜索到{}条结果。即将开始获取详细信息...'.format(len(com_url)))
        for url in com_url:
            self.get_com_info(url)

    def get_com_info(self, url):
        """获取公司的详细信息"""
        response = requests.get(url, headers=self._headers)
        html = etree.HTML(response.text)
        info_elements = html.xpath('//table[@class="ntable"]/tr')
        item = {'url': url}
        flag = True
        for element in info_elements:
            if not flag:
                break
            for index in range(0, len(element.xpath('./td')), 2):
                try:
                    key = element.xpath('./td[{}]/text()'.format(index+1))[0].strip()
                    if key == '公司介绍:' or key == '经营范围':
                        flag = False
                    if key == '法定代表人':
                        item[key] = element.xpath('./td[{}]//h2/text()'.format(index+2))[0].strip()
                    else:
                        item[key] = element.xpath('./td[{}]//text()'.format(index+2))[0].strip()
                except:
                    pass
        print(item)

    def run(self):
        """启动函数"""
        self.search(search_keyword='腾讯')


if __name__ == '__main__':
    t = QCC()
    t.run()

有问题可以留言、私信
看到就回

  • 14
    点赞
  • 75
    收藏
    觉得还不错? 一键收藏
  • 23
    评论
爬虫(Web Crawler)是一种自动化程序,用于从互联网上收集信息。其主要功能是访问网页、提取数据并存储,以便后续分析或展示。爬虫通常由搜索引擎、数据挖掘工具、监测系统等应用于网络数据抓取的场景。 爬虫的工作流程包括以下几个关键步骤: URL收集: 爬虫从一个或多个初始URL开始,递归或迭代地发现新的URL,构建一个URL队列。这些URL可以通过链接分析、站点地图、搜索引擎等方式获取。 请求网页: 爬虫使用HTTP或其他协议向目标URL发起请求,获取网页的HTML内容。这通常通过HTTP请求库实现,如Python中的Requests库。 解析内容: 爬虫对获取的HTML进行解析,提取有用的信息。常用的解析工具有正则表达式、XPath、Beautiful Soup等。这些工具帮助爬虫定位和提取目标数据,如文本、图片、链接等。 数据存储: 爬虫将提取的数据存储到数据库、文件或其他存储介质中,以备后续分析或展示。常用的存储形式包括关系型数据库、NoSQL数据库、JSON文件等。 遵守规则: 为避免对网站造成过大负担或触发反爬虫机制,爬虫需要遵守网站的robots.txt协议,限制访问频率和深度,并模拟人类访问行为,如设置User-Agent。 反爬虫应对: 由于爬虫的存在,一些网站采取了反爬虫措施,如验证码、IP封锁等。爬虫工程师需要设计相应的策略来应对这些挑战。 爬虫在各个领域都有广泛的应用,包括搜索引擎索引、数据挖掘、价格监测、新闻聚合等。然而,使用爬虫需要遵守法律和伦理规范,尊重网站的使用政策,并确保对被访问网站的服务器负责。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值