新聞中心
本篇內(nèi)容主要講解“Python怎么爬取企查查網(wǎng)站中的公司信息”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“Python怎么爬取企查查網(wǎng)站中的公司信息”吧!
成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供公安網(wǎng)站建設(shè)、公安做網(wǎng)站、公安網(wǎng)站設(shè)計(jì)、公安網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、公安企業(yè)網(wǎng)站模板建站服務(wù),十載公安做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
根據(jù)輸入的公司名稱來(lái)爬取企查查網(wǎng)站中公司的詳細(xì)信息
1、獲取headers
2、登錄成功后,可根據(jù)輸入的公司名稱進(jìn)行查詢操作,得到所需要的內(nèi)容。
3、將獲取到的文本進(jìn)行文本特殊化處理,并將其匯總成一個(gè)dataframe,方便后面保存為csv
4、輸入公司名稱
5、最后執(zhí)行此代碼,查詢companys列表中所有公司名稱的詳細(xì)信息并保存為csv。
1、獲取headers
1、進(jìn)入企查查官網(wǎng)進(jìn)行注冊(cè)并登錄。
2、然后按F12彈出開(kāi)發(fā)者工具,點(diǎn)擊Network,然后你會(huì)看到企查查這個(gè)網(wǎng)址,點(diǎn)擊一下
然后可以找到我們需要復(fù)制的header,這是非常關(guān)鍵的步驟,切記這個(gè)header是自己注冊(cè)之后登錄成功所獲取的header,這樣方便后面保存一次之后就可以在一定時(shí)間內(nèi)無(wú)限訪問(wèn)網(wǎng)址進(jìn)行查詢的操作。
from bs4 import BeautifulSoup import requests import time # 保持會(huì)話 # 新建一個(gè)session對(duì)象 sess = requests.session() # 添加headers(header為自己登錄的企查查網(wǎng)址,輸入賬號(hào)密碼登錄之后所顯示的header,此代碼的上方介紹了獲取方法) afterLogin_headers = {'User-Agent': '此代碼上方介紹了獲取的方法'} # post請(qǐng)求(代表著登錄行為,登錄一次即可保存,方便后面執(zhí)行查詢指令) login = {'user':'自己注冊(cè)的賬號(hào)','password':'密碼'} sess.post('https://www.qcc.com',data=login,headers=afterLogin_headers)
整段代碼的含義為:偽裝成用戶進(jìn)行登錄行為(返回200狀態(tài)碼代表著登錄成功)。
2、登錄成功后,可根據(jù)輸入的公司名稱進(jìn)行查詢操作,得到所需要的內(nèi)容。
def get_company_message(company): # 獲取查詢到的網(wǎng)頁(yè)內(nèi)容(全部) search = sess.get('https://www.qcc.com/search?key={}'.format(company),headers=afterLogin_headers,timeout=10) search.raise_for_status() search.encoding = 'utf-8' #linux utf-8 soup = BeautifulSoup(search.text,features="html.parser") href = soup.find_all('a',{'class': 'title'})[0].get('href') time.sleep(4) # 獲取查詢到的網(wǎng)頁(yè)內(nèi)容(全部) details = sess.get(href,headers=afterLogin_headers,timeout=10) details.raise_for_status() details.encoding = 'utf-8' #linux utf-8 details_soup = BeautifulSoup(details.text,features="html.parser") message = details_soup.text time.sleep(2) return message
上面的代碼代表著執(zhí)行了兩個(gè)步驟。
①查詢某公司
②點(diǎn)擊進(jìn)入第一位搜索結(jié)果的新網(wǎng)站,并返回該網(wǎng)址的文本內(nèi)容。
3、將獲取到的文本進(jìn)行文本特殊化處理,并將其匯總成一個(gè)dataframe,方便后面保存為csv
import pandas as pd def message_to_df(message,company): list_companys = [] Registration_status = [] Date_of_Establishment = [] registered_capital = [] contributed_capital = [] Approved_date = [] Unified_social_credit_code = [] Organization_Code = [] companyNo = [] Taxpayer_Identification_Number = [] sub_Industry = [] enterprise_type = [] Business_Term = [] Registration_Authority = [] staff_size = [] Number_of_participants = [] sub_area = [] company_adress = [] Business_Scope = [] list_companys.append(company) Registration_status.append(message.split('登記狀態(tài)')[1].split('\n')[1].split('成立日期')[0].replace(' ','')) Date_of_Establishment.append(message.split('成立日期')[1].split('\n')[1].replace(' ','')) registered_capital.append(message.split('注冊(cè)資本')[1].split('人民幣')[0].replace(' ','')) contributed_capital.append(message.split('實(shí)繳資本')[1].split('人民幣')[0].replace(' ','')) Approved_date.append(message.split('核準(zhǔn)日期')[1].split('\n')[1].replace(' ','')) try: credit = message.split('統(tǒng)一社會(huì)信用代碼')[1].split('\n')[1].replace(' ','') Unified_social_credit_code.append(credit) except: credit = message.split('統(tǒng)一社會(huì)信用代碼')[3].split('\n')[1].replace(' ','') Unified_social_credit_code.append(credit) Organization_Code.append(message.split('組織機(jī)構(gòu)代碼')[1].split('\n')[1].replace(' ','')) companyNo.append(message.split('工商注冊(cè)號(hào)')[1].split('\n')[1].replace(' ','')) Taxpayer_Identification_Number.append(message.split('納稅人識(shí)別號(hào)')[1].split('\n')[1].replace(' ','')) try: sub = message.split('所屬行業(yè)')[1].split('\n')[1].replace(' ','') sub_Industry.append(sub) except: sub = message.split('所屬行業(yè)')[1].split('為')[1].split(',')[0] sub_Industry.append(sub) enterprise_type.append(message.split('企業(yè)類型')[1].split('\n')[1].replace(' ','')) Business_Term.append(message.split('營(yíng)業(yè)期限')[1].split('登記機(jī)關(guān)')[0].split('\n')[-1].replace(' ','')) Registration_Authority.append(message.split('登記機(jī)關(guān)')[1].split('\n')[1].replace(' ','')) staff_size.append(message.split('人員規(guī)模')[1].split('人')[0].split('\n')[-1].replace(' ','')) Number_of_participants.append(message.split('參保人數(shù)')[1].split('所屬地區(qū)')[0].replace(' ','').split('\n')[2]) sub_area.append(message.split('所屬地區(qū)')[1].split('\n')[1].replace(' ','')) try: adress = message.split('經(jīng)營(yíng)范圍')[0].split('企業(yè)地址')[1].split('查看地圖')[0].split('\n')[2].replace(' ','') company_adress.append(adress) except: adress = message.split('經(jīng)營(yíng)范圍')[1].split('企業(yè)地址')[1].split()[0] company_adress.append(adress) Business_Scope.append(message.split('經(jīng)營(yíng)范圍')[1].split('\n')[1].replace(' ','')) df = pd.DataFrame({'公司':company,\ '登記狀態(tài)':Registration_status,\ '成立日期':Date_of_Establishment,\ '注冊(cè)資本':registered_capital,\ '實(shí)繳資本':contributed_capital,\ '核準(zhǔn)日期':Approved_date,\ '統(tǒng)一社會(huì)信用代碼':Unified_social_credit_code,\ '組織機(jī)構(gòu)代碼':Organization_Code,\ '工商注冊(cè)號(hào)':companyNo,\ '納稅人識(shí)別號(hào)':Taxpayer_Identification_Number,\ '所屬行業(yè)':sub_Industry,\ '企業(yè)類型':enterprise_type,\ '營(yíng)業(yè)期限':Business_Term,\ '登記機(jī)關(guān)':Registration_Authority,\ '人員規(guī)模':staff_size,\ '參保人數(shù)':Number_of_participants,\ '所屬地區(qū)':sub_area,\ '企業(yè)地址':company_adress,\ '經(jīng)營(yíng)范圍':Business_Scope}) return df
這段代碼是對(duì)獲取到的文本內(nèi)容進(jìn)行文本識(shí)別處理,只能處理大部分的內(nèi)容,可能會(huì)有極個(gè)別的是空值,大家有興趣可以自己重寫(xiě)。
4、輸入公司名稱
這里只是寫(xiě)個(gè)案例,所以隨便寫(xiě)了個(gè)列表,一般跑自己代碼的是讀取自己的csv文件關(guān)于公司名稱的那一列,然后轉(zhuǎn)為列表)
# 測(cè)試所用 companys = ['深圳市騰訊計(jì)算機(jī)系統(tǒng)有限公司','阿里巴巴(中國(guó))有限公司'] # 實(shí)際所用 # df_companys = pd.read_csv('自己目錄的絕對(duì)路徑/某某.csv') # companys = df_companys['公司名稱'].tolist()
5、最后執(zhí)行此代碼,查詢companys列表中所有公司名稱的詳細(xì)信息并保存為csv。
for company in companys: try: messages = get_company_message(company) except: pass else: df = message_to_df(messages,company) if(company==companys[0]): df.to_csv('自己目錄的絕對(duì)路徑/某某.csv',index=False,header=True) else: df.to_csv('自己目錄的絕對(duì)路徑/某某.csv',mode='a+',index=False,header=False) time.sleep(1)
至此,就可以得到這兩家公司的一些詳細(xì)信息。
ps:如果大家在 soup.find_all(‘a(chǎn)’,{‘class’: ‘title’})[0].get(‘href’)這里遇到點(diǎn)錯(cuò)誤,可能是天眼查那邊更新了網(wǎng)頁(yè)代碼,大家可以根據(jù)這個(gè)操作來(lái)更新代碼。
①按F12進(jìn)入開(kāi)發(fā)者調(diào)試頁(yè)面
③我們可以看到,這是一個(gè)a標(biāo)簽,class為title的html代碼,所以,如果報(bào)錯(cuò),可根據(jù)這個(gè)操作更換。比如,class改為了company_title,那代碼也可對(duì)應(yīng)的改為:soup.find_all(‘a(chǎn)’,{‘class’: ‘company_title’})[0].get(‘href’)
最后,大家需要注意的是,爬取的時(shí)候需要適當(dāng)?shù)脑O(shè)置一下睡眠時(shí)間,不然會(huì)被檢測(cè)到是爬蟲(chóng)機(jī)器人在操作,可能會(huì)彈出彈窗讓你驗(yàn)證,這樣會(huì)導(dǎo)致循環(huán)被中斷。第二個(gè)就是某個(gè)時(shí)間段爬取量盡量不要太大,不然也是會(huì)被檢測(cè)到的。
此處貼上完整代碼,大家可參考著學(xué)習(xí)BeautifuSoup的妙用哦。
from bs4 import BeautifulSoup import requests import time # 保持會(huì)話 # 新建一個(gè)session對(duì)象 sess = requests.session() # 添加headers(header為自己登錄的企查查網(wǎng)址,輸入賬號(hào)密碼登錄之后所顯示的header,此代碼的上方介紹了獲取方法) afterLogin_headers = {'User-Agent': '此代碼上方介紹了獲取的方法'} # post請(qǐng)求(代表著登錄行為,登錄一次即可保存,方便后面執(zhí)行查詢指令) login = {'user':'自己注冊(cè)的賬號(hào)','password':'密碼'} sess.post('https://www.qcc.com',data=login,headers=afterLogin_headers) def get_company_message(company): # 獲取查詢到的網(wǎng)頁(yè)內(nèi)容(全部) search = sess.get('https://www.qcc.com/search?key={}'.format(company),headers=afterLogin_headers,timeout=10) search.raise_for_status() search.encoding = 'utf-8' #linux utf-8 soup = BeautifulSoup(search.text,features="html.parser") href = soup.find_all('a',{'class': 'title'})[0].get('href') time.sleep(4) # 獲取查詢到的網(wǎng)頁(yè)內(nèi)容(全部) details = sess.get(href,headers=afterLogin_headers,timeout=10) details.raise_for_status() details.encoding = 'utf-8' #linux utf-8 details_soup = BeautifulSoup(details.text,features="html.parser") message = details_soup.text time.sleep(2) return message import pandas as pd def message_to_df(message,company): list_companys = [] Registration_status = [] Date_of_Establishment = [] registered_capital = [] contributed_capital = [] Approved_date = [] Unified_social_credit_code = [] Organization_Code = [] companyNo = [] Taxpayer_Identification_Number = [] sub_Industry = [] enterprise_type = [] Business_Term = [] Registration_Authority = [] staff_size = [] Number_of_participants = [] sub_area = [] company_adress = [] Business_Scope = [] list_companys.append(company) Registration_status.append(message.split('登記狀態(tài)')[1].split('\n')[1].split('成立日期')[0].replace(' ','')) Date_of_Establishment.append(message.split('成立日期')[1].split('\n')[1].replace(' ','')) registered_capital.append(message.split('注冊(cè)資本')[1].split('人民幣')[0].replace(' ','')) contributed_capital.append(message.split('實(shí)繳資本')[1].split('人民幣')[0].replace(' ','')) Approved_date.append(message.split('核準(zhǔn)日期')[1].split('\n')[1].replace(' ','')) try: credit = message.split('統(tǒng)一社會(huì)信用代碼')[1].split('\n')[1].replace(' ','') Unified_social_credit_code.append(credit) except: credit = message.split('統(tǒng)一社會(huì)信用代碼')[3].split('\n')[1].replace(' ','') Unified_social_credit_code.append(credit) Organization_Code.append(message.split('組織機(jī)構(gòu)代碼')[1].split('\n')[1].replace(' ','')) companyNo.append(message.split('工商注冊(cè)號(hào)')[1].split('\n')[1].replace(' ','')) Taxpayer_Identification_Number.append(message.split('納稅人識(shí)別號(hào)')[1].split('\n')[1].replace(' ','')) try: sub = message.split('所屬行業(yè)')[1].split('\n')[1].replace(' ','') sub_Industry.append(sub) except: sub = message.split('所屬行業(yè)')[1].split('為')[1].split(',')[0] sub_Industry.append(sub) enterprise_type.append(message.split('企業(yè)類型')[1].split('\n')[1].replace(' ','')) Business_Term.append(message.split('營(yíng)業(yè)期限')[1].split('登記機(jī)關(guān)')[0].split('\n')[-1].replace(' ','')) Registration_Authority.append(message.split('登記機(jī)關(guān)')[1].split('\n')[1].replace(' ','')) staff_size.append(message.split('人員規(guī)模')[1].split('人')[0].split('\n')[-1].replace(' ','')) Number_of_participants.append(message.split('參保人數(shù)')[1].split('所屬地區(qū)')[0].replace(' ','').split('\n')[2]) sub_area.append(message.split('所屬地區(qū)')[1].split('\n')[1].replace(' ','')) try: adress = message.split('經(jīng)營(yíng)范圍')[0].split('企業(yè)地址')[1].split('查看地圖')[0].split('\n')[2].replace(' ','') company_adress.append(adress) except: adress = message.split('經(jīng)營(yíng)范圍')[1].split('企業(yè)地址')[1].split()[0] company_adress.append(adress) Business_Scope.append(message.split('經(jīng)營(yíng)范圍')[1].split('\n')[1].replace(' ','')) df = pd.DataFrame({'公司':company,\ '登記狀態(tài)':Registration_status,\ '成立日期':Date_of_Establishment,\ '注冊(cè)資本':registered_capital,\ '實(shí)繳資本':contributed_capital,\ '核準(zhǔn)日期':Approved_date,\ '統(tǒng)一社會(huì)信用代碼':Unified_social_credit_code,\ '組織機(jī)構(gòu)代碼':Organization_Code,\ '工商注冊(cè)號(hào)':companyNo,\ '納稅人識(shí)別號(hào)':Taxpayer_Identification_Number,\ '所屬行業(yè)':sub_Industry,\ '企業(yè)類型':enterprise_type,\ '營(yíng)業(yè)期限':Business_Term,\ '登記機(jī)關(guān)':Registration_Authority,\ '人員規(guī)模':staff_size,\ '參保人數(shù)':Number_of_participants,\ '所屬地區(qū)':sub_area,\ '企業(yè)地址':company_adress,\ '經(jīng)營(yíng)范圍':Business_Scope}) return df # 測(cè)試所用 companys = ['深圳市騰訊計(jì)算機(jī)系統(tǒng)有限公司','阿里巴巴(中國(guó))有限公司'] # 實(shí)際所用 # df_companys = pd.read_csv('自己目錄的絕對(duì)路徑/某某.csv') # companys = df_companys['公司名稱'].tolist() for company in companys: try: messages = get_company_message(company) except: pass else: df = message_to_df(messages,company) if(company==companys[0]): df.to_csv('自己目錄的絕對(duì)路徑/某某.csv',index=False,header=True) else: df.to_csv('自己目錄的絕對(duì)路徑/某某.csv',mode='a+',index=False,header=False) time.sleep(1)
到此,相信大家對(duì)“Python怎么爬取企查查網(wǎng)站中的公司信息”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
文章名稱:Python怎么爬取企查查網(wǎng)站中的公司信息
文章源于:http://fisionsoft.com.cn/article/jdeogc.html