根据excel中的企业列表数据爬取对应的营业范围信息,并写入excel文件。
from selenium import webdriver
from selenium.webdriver.chrome.service import Service # 新增
from selenium.webdriver.common.by import By
from selenium.webdriver import Chrome, ChromeOptions
import openpyxl as op
from selenium.webdriver import *
import time
from openpyxl import load_workbook
# 声明调用哪个浏览器,本文使用的是Chrome,其他浏览器同理
#没有把Chromedriver放到python安装路径
service = Service(executable_path=r'C:\Users\Administrator\Desktop\chromedriver.exe')
driver = webdriver.Chrome(service=service)
#爬取网址
url = "https://www.qcc.com"
driver.get(url)
driver.page_source
print("已打开浏览器")
driver.implicitly_wait(60)
print("等待加载完成")
#30s的时间用来在网站打开时手动登陆
time.sleep(30)
print('开始主程序')
time_start = time.time()
#获取公司名称
wb = op.load_workbook(r'C:\Users\Administrator\Desktop\XXXXX.xlsx')
ws = wb['Sheet1']
m_row = ws.max_row
print('一共' + str(m_row) + '个公司需要爬取')
j = 0
for i in range(m_row):
try:
company_name = ws.cell(i+1, 1).value
# 搜索框输入
driver.find_elements(By.ID, "searchKey")[0].clear()
driver.find_elements(By.ID, "searchKey")[0].send_keys(company_name)
#模拟输入回车按键
from selenium.webdriver.common.keys import Keys
driver.find_elements(By.CLASS_NAME, "input-group-btn")[0].click()
driver.implicitly_wait(10)
time.sleep(2)
# 获取主窗口
main_window = driver.current_window_handle
#点击【公司名称】
company_title = driver.find_element(By.CLASS_NAME, "copy-value")
company_title.click()
#关闭旧窗口
driver.close()
# 切换到新窗口
for window in driver.window_handles:
if window != main_window:
driver.switch_to.window(window)
break
time.sleep(2)
spyder_text = driver.find_elements(By.CLASS_NAME, "copy-value")[-1].text
print(spyder_text)
ws.cell(i+3, 3).value = spyder_text
print('第' + str(i+1) + '个爬取完成,还剩' + str(m_row-i) + '个')
except Exception as e:
j = j + 1
print('报错')
ws.cell(i + 1, 3).value = '报错'
continue
wb.save(r'C:\Users\Administrator\Desktop\南京API展会企业名单_爬取完成.xlsx')
time_end = time.time()
print('全部爬取并写入完成,失败个数:' + str(j) + ' 总用时' + str('%.2f'%(time_end-time_start)) + '秒')
本文转载自: https://blog.csdn.net/zwlalalala/article/details/133799444
版权归原作者 随便写写python 所有, 如有侵权,请联系我们删除。
版权归原作者 随便写写python 所有, 如有侵权,请联系我们删除。