用python写一段代码实现这些功能:我的excel里第一列有一百家公司名,运行程序后,自动在指定网站上搜索第一列公司地址,输出地址在第二列
时间: 2023-03-14 12:32:44 浏览: 86
可以这样写:import requests
from bs4 import BeautifulSoup# 在第一列中获取100家公司的列表
companies = [c1, c2, c3, ... , c100]# 定义一个函数,用来获取每个公司的地址
def get_address(company):
url = 'http://www.example.com/search?q=' + company
page = requests.get(url)
soup = BeautifulSoup(page.text, 'html.parser')
address = soup.find('div', {'class': 'address'}).text
return address# 遍历每个公司,获取地址,并将结果写入excel的第二列
for company in companies:
address = get_address(company)
# 将地址写入第二列
excel.write(column=2, row=company, data=address)
相关问题
用python写一段代码实现这些功能:名为ABC的excel文件里第一列有一百家公司名,运行程序后,自动在指定网站上搜索第一列公司地址,输出地址在第二列
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')
写一段python程序:文件夹G:\drought\data\data1\stationhh下保存着txt文件,把每个文件转换成excel表格,再删除每个表格的第4列、6-10列、12-16列(保留1、2、3、5、11列)。
以下是一个Python脚本的示例,该脚本会遍历指定文件夹下的所有txt文件,将它们转换为Excel表格,并删除指定的列。在这个例子中,我使用了`pandas`库来处理数据,以及`openpyxl`库来操作Excel文件。请确保在运行此脚本之前已经安装了这两个库,否则需要使用`pip install pandas openpyxl`进行安装。
```python
import os
import pandas as pd
# 指定文件夹路径
folder_path = r'G:\drought\data\data1\stationhh'
# 获取该文件夹下所有的txt文件
txt_files = [f for f in os.listdir(folder_path) if f.endswith('.txt')]
# 遍历所有txt文件
for txt_file in txt_files:
# 构建完整的文件路径
file_path = os.path.join(folder_path, txt_file)
# 读取txt文件到DataFrame
df = pd.read_csv(file_path, sep='\t', header=0)
# 定义要保留的列名
columns_to_keep = [df.columns[i] for i in [0, 1, 2, 4] + list(range(5, 11))]
# 保留指定的列
df = df[columns_to_keep]
# 构建新的Excel文件名并保存
excel_file = txt_file.rsplit('.', 1)[0] + '.xlsx'
excel_path = os.path.join(folder_path, excel_file)
df.to_excel(excel_path, index=False)
print("转换完成。")
```
这个脚本执行了以下步骤:
1. 指定了包含txt文件的文件夹路径。
2. 读取该文件夹下的所有txt文件。
3. 对每个txt文件,使用pandas的`read_csv`方法读取数据到DataFrame,其中`sep='\t'`表示字段是用制表符分隔的,`header=0`表示第一行是列名。
4. 创建了一个列名列表,只包含要保留的列名。
5. 根据这个列表保留DataFrame中的列。
6. 将处理后的DataFrame保存为Excel文件(不包含索引)。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)