用python写一段代码实现这些功能:名为ABC的excel文件里第一列有一百家公司名,运行程序后,自动在指定网站上搜索第一列公司地址,输出地址在第二列
时间: 2023-03-14 18:32:44 浏览: 51
import requests
from bs4 import BeautifulSoup
import openpyxl# 打开文件
wb = openpyxl.load_workbook('ABC.xlsx')
# 选择表单
sheet = wb['Sheet1']
for i in range(1, sheet.max_row+1):
# 获取第一列内容
company_name = sheet.cell(row=i, column=1).value
# 拼接搜索链接
search_url = f'https://www.example.com/search?q={company_name}'
# 请求链接
r = requests.get(search_url)
# 解析页面
soup = BeautifulSoup(r.text, 'lxml')
# 获取公司地址
company_address = soup.select_one('div.company_address').get_text()
# 写入第二列
sheet.cell(row=i, column=2).value = company_address# 保存文件
wb.save('ABC.xlsx')