读书笔记(十)——python简单爬取企查查网企业信息,并以excel格式存储

今天这个小爬虫是应朋友,帮忙写的一个简单的爬虫,目的是爬取企查查这个网站的企业信息。javascript

编程最终要的就是搭建编程环境,这里咱们的编程环境是:html

  • python3.6
  • BeautifulSoup模块
  • lxml模块
  • requests模块
  • xlwt模块
  • geany

首先分析需求网页的信息:java

http://www.qichacha.com/search?key=婚庆

能够看到咱们想要提取的消息内容有公司的名字,法定表明人,注册资本,成立时间,电话,邮箱,地址。好的,接下来咱们打开firebug,查看各个内容在网页中的具体位置:python

能够看到这些消息分别位于:chrome

#公司名字------<a class="ma_h1" href="/firm_8c640ea3b396783ab4e013ea5f7f295e.html" target="_blank">
#               昆明嘉馨
#               <em>
#               有限公司
#               </a>
#法定表明人----<p class="m-t-xs">
#                 法定表明人:
#                 <a class="a-blue" href="********">鄢显莉</a>
#注册资本----       <span class="m-l">注册资本:100万</span>
#成立时间----       <span class="m-l">成立时间:2002-05-20</span>
#            </p>
#            <p class="m-t-xs">
#联系方式----       电话:13888677871
#公司邮箱----       <span class="m-l">邮箱:-</span>
#            </p>
#公司地址----  <p class="m-t-xs"> 地址:昆明市南屏街88号世纪广场B2幢12楼A+F号 </p>
#            <p></p>

可是有一个巨大的问题摆在咱们面前,企查查在点击搜索按钮后,虽然也能呈现部分资料,可是首当其冲的是一个登陆页面,在没有登陆前,咱们实际上经过爬虫访问到的是仅有前五个公司信息+登陆窗口的网页编程

若是咱们不处理这个登陆页面,那么很抱歉,此次爬取到此结束了。浏览器

因此咱们必须处理这个问题,首先须要在企查查上注册一个账号,注册步骤略,通常能够经过安全

  • 构造请求头,配置cookies
  • 使用selenium
  • requests.post去递交用户名密码等

selenium模拟真实的浏览器去访问页面,可是其访问速度又慢,还要等加载完成,容易报错,直接放弃。服务器

requests.post方法,这个可能能够,没仔细研究,由于企查查登陆涉及三个选项,第一个是手机号,第二个是您的密码,第三个是一个滑块,滑块估计须要构造一个True或者什么东西吧。cookie

第一先想确定是构造请求头,配置一个cookies。在这里我要说明本身犯的一个错误,User-Agent写成了User_agent,致使个人请求头是错误的,访问获得的是一个被防火墙拦截的网页页面,以下:

#-*- coding-8 -*-
import requests
import lxml
from bs4 import BeautifulSoup

def craw(url):
    user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0'
    headers = {'User-Agent':user_agent,

}
    response = requests.get(url,headers = headers)
    if response.status_code != 200:
        response.encoding = 'utf-8'
        print(response.status_code)
        print('ERROR')

    soup = BeautifulSoup(response.text,'lxml')
    print(soup)
if __name__ == '__main__':
    url = r'http://www.qichacha.com/search?key=%E5%A9%9A%E5%BA%86'
    s1 = craw(url)

代码仅仅是输出soup,方便调试,请求状态是一个405错误,获得的页面以下:

<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8"/>
<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible"/>
<meta content="a3c0e" name="data-spm"/>
<title>405</title>
<style>
            html, body, div, a, h2, p { margin: 0; padding: 0; font-family: 微软
雅黑; }
            a { text-decoration: none; color: #3b6ea3;  }
            .container { width: 1000px; margin: auto; color: #696969; }


            .header { padding: 50px 0; }
            .header .message { height: 36px; padding-left: 120px; background: ur
l(https://errors.aliyun.com/images/TB1TpamHpXXXXaJXXXXeB7nYVXX-104-162.png) no-r
epeat 0 -128px; line-height: 36px; }

            .main { padding: 50px 0; background: #f4f5f7; }
            .main img { position: relative; left: 120px; }

            .footer { margin-top: 30px; text-align: right; }
            .footer a { padding: 8px 30px; border-radius: 10px; border: 1px soli
d #4babec; }
            .footer a:hover { opacity: .8; }

            .alert-shadow { display: none; position: absolute; top: 0; left: 0;
width: 100%; height: 100%; background: #999; opacity: .5; }
            .alert { display: none; position: absolute; top: 200px; left: 50%; w
idth: 600px; margin-left: -300px; padding-bottom: 25px; border: 1px solid #ddd;
box-shadow: 0 2px 2px 1px rgba(0, 0, 0, .1); background: #fff; font-size: 14px;
color: #696969; }
            .alert h2 {  margin: 0 2px; padding: 10px 15px 5px 15px; font-size:
14px; font-weight: normal; border-bottom: 1px solid #ddd; }
            .alert a { display: block; position: absolute; right: 10px; top: 8px
; width: 30px; height: 20px; text-align: center; }
            .alert p {  padding: 20px 15px; }
        </style>
</head>
<body data-spm="7663354">
<div data-spm="1998410538">
<div class="header">
<div class="container">
<div class="message">很抱歉,因为您访问的URL有可能对网站形成安全威胁,您的访问被
阻断。</div>
</div>
</div>
<div class="main">
<div class="container">
<img src="https://errors.aliyun.com/images/TB15QGaHpXXXXXOaXXXXia39XXX-660-117.p
ng"/>
</div>
</div>
<div class="footer">
<div class="container">
<a data-spm-click="gostr=/waf.123.123;locaid=d001;" href="javascript:;" id="repo
rt" target="_blank">误报反馈</a>
</div>
</div>
</div>
<div class="alert-shadow" id="alertShadow"></div>
<div class="alert" id="alertContainer">
<h2>提示:<a href="javascript:;" id="closeAlert" title="关闭">X</a></h2>
<p>感谢您的反馈,应用防火墙会尽快进行分析和确认。</p>
</div>
<script>
             function show() {

                var g = function(ele) { return document.getElementById(ele); };
                var reportHandle = g('report');
                var alertShadow = g('alertShadow');
                var alertContainer = g('alertContainer');
                var closeAlert = g('closeAlert');

                var own = {};

                own.report = function() {
                    // SPM
                    own.alert();
                };

                own.alert = function() {
                    alertShadow.style.display = 'block';
                    alertContainer.style.display = 'block';
                };

                own.close = function() {
                    alertShadow.style.display = 'none';
                    alertContainer.style.display = 'none';
                };

            };
        </script>
<script charset="utf-8" src="https://errors.aliyun.com/error.js?s=3" type="text/
javascript"></script>
</body>
</html>

这个错误也说明了请求头的重要性,这通常是服务器根据你的请求头来简单判断你是一个攻击者、爬虫,仍是一个正常访问的人。因此干脆直接把请求头整个复制下来。

这边还有一点要注意,就是你使用的浏览器需打开COOKIES功能,并且关闭浏览器的时候不能自动或守清除cookies,不然都会致使只能获得前五个公司的信息,剩下的仍是登录消息。

直接上代码,一点点的记录:

#-*- coding-8 -*-
import requests
import lxml
from bs4 import BeautifulSoup
import xlwt


def craw(url):
    user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0'
    headers = {
'Host':'www.qichacha.com',
'User-Agent':r'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0',
'Accept':'*/*',
'Accept-Language':'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding':'gzip, deflate',
'Referer':'http://www.qichacha.com/',
'Cookie':r'UM_distinctid***************',
'Connection':'keep-alive',
'If-Modified-Since':'Wed, 30 **********',
'If-None-Match':'"59*******"',
'Cache-Control':'max-age=0',

}
    response = requests.get(url,headers = headers)
    if response.status_code != 200:
        response.encoding = 'utf-8'
        print(response.status_code)
        print('ERROR')    
    soup = BeautifulSoup(response.text,'lxml')
    #print(soup)
    com_names = soup.find_all(class_='ma_h1')
    #print(com_names)
    #com_name1 = com_names[1].get_text()
    #print(com_name1)
    peo_names = soup.find_all(class_='a-blue')
    #print(peo_names)
    peo_phones = soup.find_all(class_='m-t-xs')
    #tags = peo_phones[4].find(text = True).strip()
    #print(tags)
    #tttt = peo_phones[0].contents[5].get_text()
    #print (tttt)
    #else_comtent = peo_phones[0].find(class_='m-l')
    #print(else_comtent)
    global com_name_list
    global peo_name_list
    global peo_phone_list
    global com_place_list
    global zhuceziben_list
    global chenglishijian_list
    print('开始爬取数据,请勿打开excel')
    for i in range(0,len(com_names)):
        n = 1+3*i
        m = i+2*(i+1)
        peo_phone = peo_phones[n].find(text = True).strip()
        com_place = peo_phones[m].find(text = True).strip()
        zhuceziben = peo_phones[3*i].find(class_='m-l').get_text()
        chenglishijian = peo_phones[3*i].contents[5].get_text()
        peo_phone_list.append(peo_phone)
        com_place_list.append(com_place)   
        zhuceziben_list.append(zhuceziben)
        chenglishijian_list.append(chenglishijian)
    for com_name,peo_name in zip(com_names,peo_names):
        com_name = com_name.get_text()
        peo_name = peo_name.get_text()
        com_name_list.append(com_name)
        peo_name_list.append(peo_name)
        
    

        
if __name__ == '__main__':
    com_name_list = []
    peo_name_list = []
    peo_phone_list = []
    com_place_list = []
    zhuceziben_list = []
    chenglishijian_list = []
    key_word = input('请输入您想搜索的关键词:')
    print('正在搜索,请稍后')
    for x in range(1,11):
        url = r'http://www.qichacha.com/search?key={}#p:{}&'.format(key_word,x)
        s1 = craw(url)
    workbook = xlwt.Workbook()
    #建立sheet对象,新建sheet
    sheet1 = workbook.add_sheet('xlwt', cell_overwrite_ok=True)
    #---设置excel样式---
    #初始化样式
    style = xlwt.XFStyle()
    #建立字体样式
    font = xlwt.Font()
    font.name = 'Times New Roman'
    font.bold = True #加粗
    #设置字体
    style.font = font
    #使用样式写入数据
    # sheet.write(0, 1, "xxxxx", style)
    print('正在存储数据,请勿打开excel')
    #向sheet中写入数据
    name_list = ['公司名字','法定表明人','联系方式','注册人资本','成立时间','公司地址']
    for cc in range(0,len(name_list)):
        sheet1.write(0,cc,name_list[cc],style)
    for i in range(0,len(com_name_list)):
        sheet1.write(i+1,0,com_name_list[i],style)#公司名字
        sheet1.write(i+1,1,peo_name_list[i],style)#法定表明人
        sheet1.write(i+1,2,peo_phone_list[i],style)#联系方式
        sheet1.write(i+1,3,zhuceziben_list[i],style)#注册人资本
        sheet1.write(i+1,4,chenglishijian_list[i],style)#成立时间
        sheet1.write(i+1,5,com_place_list[i],style)#公司地址
    #保存excel文件,有同名的直接覆盖
    workbook.save(r'F:\work\2017_08_02\xlwt.xls')
    print('the excel save success')

首先咱们引入requests、BeautifulSoup、lxml、xlwt四个模块。

#-*- coding-8 -*-
import requests
import lxml
from bs4 import BeautifulSoup
import xlwt

简要说明一下四个模块:

  • requests是一个第三方模块,源码位于Github上,它相对于urrllib/httplib更加的人性化,通常推荐使用这个,requests具备多种请求方式。
    import requests
    r1 = requests.get(r'http://www.baidu.com')
    postdata = {'key':'value'}
    r2 = requests.post(r'http://www.xxx.com/login',data=postdata)
    r3 = requests.put(r'http://www.xxx.com/put',data={'key':'value'})
    r4 = requests.delete(r'http://www.xxx.com/delete')
    r5 = requests.head(r'http://www.xxx.com/get')
    r6 = requests.options(r'http://www.xxx.com/get')

    还要说明一点,就是其响应编码:

    import requests
    r = requests.get(r'http://www.baidu.com')
    print(r.content)#返回的是字节形式
    print(r.text)#返回的是文本形式
    print(r.encoding)#根据HTTP头猜想的网页编码格式,能够直接赋值修改

    更多的requests后续找个机会补充。

  • BeautifulSoup这是一个能够从HTML或XML文件中提取数据的python库,它会把HTML转换成文档树,既然是树形结构,它必有节点概念,便于在爬虫中使用它的查找提取功能,它的这个功能通常有两种方法:1、find、find_all等方法;2、CSS选择器。

  • lxml模块,这是使用XPath技术查询和处理处理HTML/XML文档的库,只会局部遍历,因此速度会更快,占用的内存开销也会比较小。

  • xlwt模块,这是一个写成excel的模块,可是它只能从新生成一个excel,也就是说,若是在这个路径下,已经有这个excel了,那么就会直接覆盖掉这个excel,并且这个模块不支持读取。若是须要读取功能的能够用xlrd,而写入功能能够用xlutils模块配合着xlrd模块使用,具体我建议能够看看这篇博客《Python excel读写

接下来就很简单了,定义函数,构造请求头,requests访问网页,若是请求相应码不是200,则输出对应的相应码以及‘ERROR’,用BeautifulSoup和lxml解析网页,从网页中选出所要的信息,定义6个全局变量列表,搜索到的数据经过列表的方法append加入列表。

def craw(url):
    user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0'
    headers = {
'Host':'www.qichacha.com',
'User-Agent':r'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0',
'Accept':'*/*',
'Accept-Language':'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding':'gzip, deflate',
'Referer':'http://www.qichacha.com/',
'Cookie':r'UM_di**********1',
'Connection':'keep-alive',
'If-Modified-Since':'Wed, *********',
'If-None-Match':'"59****"',
'Cache-Control':'max-age=0',

}
    response = requests.get(url,headers = headers)
    if response.status_code != 200:
        response.encoding = 'utf-8'
        print(response.status_code)
        print('ERROR')    
    soup = BeautifulSoup(response.text,'lxml')
    #print(soup)
    com_names = soup.find_all(class_='ma_h1')
    #print(com_names)
    #com_name1 = com_names[1].get_text()
    #print(com_name1)
    peo_names = soup.find_all(class_='a-blue')
    #print(peo_names)
    peo_phones = soup.find_all(class_='m-t-xs')
    #tags = peo_phones[4].find(text = True).strip()
    #print(tags)
    #tttt = peo_phones[0].contents[5].get_text()
    #print (tttt)
    #else_comtent = peo_phones[0].find(class_='m-l')
    #print(else_comtent)
    global com_name_list
    global peo_name_list
    global peo_phone_list
    global com_place_list
    global zhuceziben_list
    global chenglishijian_list
    print('开始爬取数据,请勿打开excel')
    for i in range(0,len(com_names)):
        n = 1+3*i
        m = i+2*(i+1)
        peo_phone = peo_phones[n].find(text = True).strip()
        com_place = peo_phones[m].find(text = True).strip()
        zhuceziben = peo_phones[3*i].find(class_='m-l').get_text()
        chenglishijian = peo_phones[3*i].contents[5].get_text()
        peo_phone_list.append(peo_phone)
        com_place_list.append(com_place)   
        zhuceziben_list.append(zhuceziben)
        chenglishijian_list.append(chenglishijian)
    for com_name,peo_name in zip(com_names,peo_names):
        com_name = com_name.get_text()
        peo_name = peo_name.get_text()
        com_name_list.append(com_name)
        peo_name_list.append(peo_name)

经过不断的调用函数craw,不断的往list中添加数据,由于企查查非会员只能查看十页的数据,因此咱们只须要重复十次便可,这边的range()有一个须要注意的地方,由于通常range都是从0开始循环的,可是网页的第一页就是1(比较网站的url,尤为是第一页的url和第二页的url更容易发现),因此若是咱们须要循环十次,那么就须要从1开始,10是最后一次,11是截至,因此须要这么写:rang(1,11)。接下来的就是建立sheet对象,新建sheet,定义sheet的样式,而后经过for循环不断的往excel中存储数据,最后再经过方法save()保存到某个路径下。

if __name__ == '__main__':
    com_name_list = []
    peo_name_list = []
    peo_phone_list = []
    com_place_list = []
    zhuceziben_list = []
    chenglishijian_list = []
    key_word = input('请输入您想搜索的关键词:')
    print('正在搜索,请稍后')
    for x in range(1,11):
        url = r'http://www.qichacha.com/search?key={}#p:{}&'.format(key_word,x)
        s1 = craw(url)
    workbook = xlwt.Workbook()
    #建立sheet对象,新建sheet
    sheet1 = workbook.add_sheet('xlwt', cell_overwrite_ok=True)
    #---设置excel样式---
    #初始化样式
    style = xlwt.XFStyle()
    #建立字体样式
    font = xlwt.Font()
    font.name = 'Times New Roman'
    font.bold = True #加粗
    #设置字体
    style.font = font
    #使用样式写入数据
    # sheet.write(0, 1, "xxxxx", style)
    print('正在存储数据,请勿打开excel')
    #向sheet中写入数据
    name_list = ['公司名字','法定表明人','联系方式','注册人资本','成立时间','公司地址']
    for cc in range(0,len(name_list)):
        sheet1.write(0,cc,name_list[cc],style)
    for i in range(0,len(com_name_list)):
        sheet1.write(i+1,0,com_name_list[i],style)#公司名字
        sheet1.write(i+1,1,peo_name_list[i],style)#法定表明人
        sheet1.write(i+1,2,peo_phone_list[i],style)#联系方式
        sheet1.write(i+1,3,zhuceziben_list[i],style)#注册人资本
        sheet1.write(i+1,4,chenglishijian_list[i],style)#成立时间
        sheet1.write(i+1,5,com_place_list[i],style)#公司地址
    #保存excel文件,有同名的直接覆盖
    workbook.save(r'F:\work\2017_08_02\xlwt.xls')
    print('the excel save success')

代码基本上到这边结束了,爬取效果也还能够。以前只作了个半成品,只处理一页的数据,并无完善整个功能,后续加了翻页,完善了存储功能。

相关文章
相关标签/搜索