admin 管理员组

文章数量: 1086019


2024年3月11日发(作者:随机数发生器有什么用)

给定公司名称excel列表,在天眼查搜索爬取企业工商信息

上一篇写了天眼查公司详情页单页面爬取公司基本信

息(简单爬取天眼查数据),这里改进一步。需求提供公司

名称,需要获取公司工商注册信息,欲查找的公司名称存储

在excel里。步骤分解1.读取excel数据(1)打开工作簿import

xlrddef open_excel(: book =

_workbook( bookexcept Exception as e:print ('打开

工作簿'+file+'出错:'+str(e))(2)读取工作簿中所有工作表

def read_sheets(: book = open_excel(file) sheets =

()return sheetsexcept Exception as e:print ('读取

工作表出错:'+str(e))(3)读取某一工作表中数据某一列的

数据def read_data(sheet, n=0): dataset = []for r in

range(): col = (r, n).value

(col)return dataset2.打开浏览器因为是采用

selenium+浏览器,首先先打开浏览器(可用火狐、谷歌等

浏览器,只要配置好环境),这里采用了phantomjs这个无

头浏览器,并使用了useragent代理。此外,service_args参

数可调用ip代理。from selenium import webdriverfrom

d_capabilities import

DesiredCapabilitiesdef driver_open(): dcap =

dict(MJS)

dcap['ent'] = ('Mozilla/5.0

(Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML,

like Gecko) Chrome/52.0.2743.82 Safari/537.36' )

service_args =

['--proxy=122.228.179.178:80','--proxy-type=http' ]

open_driver =

mJS(executable_path='D:/Anaconda2/pha

', desired_capabilities=dcap# ,

service_args=service_args # ip代理 )return open_driver3.获

取网页源代码根据提供的url和等待加载时间,返回网页源

码的BeautifulSoup对象。欲获取源代码的网页url有两种类

型,一种是搜索结果页面的url,一种是公司详情页的url。

根据分析,公司详情页的url末尾是一串数字,表示公司的

id,这个id是要保存下来,作为唯一识别码,因此增加一步

判断,判断url是否为公司详情页的url(最后一个'/'后面全

是数字), 如果是,同时返回公司的id。import timefrom bs4

import BeautifulSoupdef get_content(url, waiting=3):

open_driver = driver_open() open_(url)# 等待

waiting秒,使js加载,时间可延长 (waiting)# 获

取网页内容 content =

open__('utf-8')print content

open_() res_soup = BeautifulSoup(content, 'lxml')

com_id = ('/')[-1]if com_t():return res_soup,


本文标签: 公司 浏览器 打开 爬取