1. 程式人生 > 實用技巧 >python爬取某查查用selenium終於搞定了!

python爬取某查查用selenium終於搞定了!

原文連結

之前試過用request的方法爬取企查查, 結果完全獲取不到資訊。經論壇朋友們指點,找到selenium神器,學了兩天,終於可以啦!
怕被封號,所以設定了很多等待時間,爬取速度比較慢。


程式碼如下:

[Python] 純文字檢視 複製程式碼 ?
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 from selenium import webdriver import time from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.keys import Keys import xlrd from xlutils.copy import copy import random ####################################模擬使用IE瀏覽器登陸#################################### option
= webdriver.IeOptions() option.add_argument('--user-agent="Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"') path = 'C:\Program Files (x86)\Internet Explorer\IEDriverServer.exe' driver = webdriver.Ie(executable_path=path, options=option) driver.implicitly_wait(10) ####################################通過名稱獲取網頁####################################
#開啟搜尋頁 driver.get('https://www.qcc.com/search?key=%E6%B1%9F%E5%AE%81%E5%8E%BF%E8%93%9D%E5%A4%A9%E7%A0%96%E5%8E%82') time.sleep(20) # 20s內手動登入 #輸入搜尋詞 #corps = ['廣州海洋勘探開發總公司', '廣東車海洋環保科技有限公司', '廣東粵新海洋工程裝備股份有限公司', '廣東省海洋工程職業技術學校(廣東省海洋工程技工學校)',] #'廣東華風海洋資訊系統服務有限公司', '廣州海洋天網科技股份有限公司', '中船海洋與防務裝備股份有限公司', '廣州海洋生物科普有限公司'] for i in range(0, len(corps)): corp = corps[i] #清空搜尋框 driver.find_element_by_id('headerKey').clear() #向搜尋框輸入搜尋詞 driver.find_element_by_id('headerKey').send_keys(corp) time.sleep(6) try: #獲取公司連結 link = driver.find_element_by_xpath('//a[@class="list-group-item keyMoveItem"]').get_attribute("href") #開啟公司連結 driver.get(link) time.sleep(6) except: print(corp + ':未查詢到資訊!') ####################################獲取資訊#################################### #獲取子介面 elements = driver.find_elements_by_class_name('company-nav-head') for element in elements: ele = element.get_attribute('href') #點選基本資訊 if 'base' in ele: element.click() #獲取公司名稱 corpname = driver.find_element_by_xpath('//div[@class="row title jk-tip"]/h1').text #獲取所屬行業 try: hangye = driver.find_element_by_xpath('//table[@class="ntable"]//tr[4]/td[6]').text except: hangye = 'None' #獲取人員規模 try: renyuan = driver.find_element_by_xpath('//table[@class="ntable"]//tr[6]/td[2]').text except: renyuan = 'None' #獲取經營範圍 try: jingying = driver.find_element_by_xpath('//table[@class="ntable"]//tr[9]/td[2]').text except: jingying = 'None' #獲取標籤 try: tags = driver.find_elements_by_xpath('//div[@class="row tags"]/span') tagname = [] for tag in tags: tagname.append(tag.text) tagname = "、".join(tagname) except: zl = "None" #print(corpname + '-'+ hangye + '-'+ renyuan + '-'+ jingying + '-'+ tagname) #點選專利資訊 elif 'assets' in ele: element.click() #獲取科技創新含量 try: techscore = driver.find_element_by_xpath('//div[@class="zlchart-tcount"]/span').text except: techscore = "None" #獲取專利數量 try: zlcount = driver.find_element_by_xpath('//aspan[@class="title"]/span[@class="tbadge"]').text except: zlcount = "None" #獲取專利資訊 try: zlments = driver.find_elements_by_xpath('//section[@id="zhuanlilist"]//table[@class="ntable ntable-odd"]//tr/td[5]') zls = [] for zlment in zlments: zls.append(zlment.text) zl = "、".join(zls) except: zl = "None" #print(corpname + '-' + techscore + '-' + zlcount + '-' + zl) else: pass time.sleep(4) ####################################儲存資訊#################################### #儲存到excel中 rexcel = xlrd.open_workbook("C:/Users/SNT/Desktop/qcc.xls") table = rexcel.sheets()[0] # 開啟第一個sheet rows = table.nrows # 獲取行數 # 用xlutils方法追寫excel excel = copy(rexcel) # 用xlutis提供的copy方法將xlrd物件轉化為xlwt物件 sheet = excel.get_sheet(0) # 獲取第一個sheet sheet.write(rows, 0, rows) sheet.write(rows, 1, corpname) sheet.write(rows, 2, tagname) sheet.write(rows, 3, hangye) sheet.write(rows, 4, renyuan) sheet.write(rows, 5, jingying) sheet.write(rows, 6, techscore) sheet.write(rows, 7, zlcount) sheet.write(rows, 8, zl) sheet.write(rows, 9, corp) excel.save("C:/Users/SNT/Desktop/qcc.xls") print(corp +'資訊儲存完畢!') sltime = 30*random.random() print('休息' + str(sltime) +'s......\n') time.sleep(sltime) print(str(i+1) +'家公司資訊儲存完畢') #關閉瀏覽器 driver.quit()




執行結果:

免費評分