通过邮编查地址 您所在的位置:网站首页 邮编库街道办查询 通过邮编查地址

通过邮编查地址

2023-07-08 14:55| 来源: 网络整理| 查看: 265

帮对象处理所需数据时写的代码——第二弹(实现功能:可以快速实现查询excel表格里所有邮编对应的地址信息,将输出的省市县信息放在一起)

目录

第 1 节:导入库

第 2 节:检索位置信息的函数

第 3 节:输入和输出文件路径

第 4 节:创建和配置输出 Excel 文件

第 5 节:加载和处理输入 Excel 文件

第 6 节:处理输入表中的行并保存输出

第 7 节:代码总结

第 8 节:代码运行实例

完整程序:

第 1 节:导入库

本节导入程序所需的库。它包括requests用于发出 HTTP 请求、openpyxl用于处理 Excel 文件、lxml用于 HTML 解析以及tqdm用于进度条可视化。

import requests import openpyxl from lxml import etree from tqdm import tqdm 第 2 节:检索位置信息的函数

本节定义了get_location以 apostal_code作为输入的函数。它使用提供的邮政编码向网站“ https://www.youbianku.com/ ”发送 HTTP GET 请求。然后,它使用 XPath 从 HTML 响应中提取位置信息。如果找到位置信息,则返回;否则,返回“无位置信息”(无位置信息)。

def get_location(postal_code): url = f"https://www.youbianku.com/{postal_code}" response = requests.get(url) html = etree.HTML(response.text) location_element = html.xpath('/html/body/div[1]/h1') if location_element: return location_element[0].text else: return "无位置信息" 第 3 节:输入和输出文件路径

在本节中,指定输入和输出文件路径。input_file保存输入 Excel 文件的路径,同时output_file保存将创建的输出 Excel 文件的路径。

# Load the Excel file input_file = r'C:\Users\aa\Desktop\11.xlsx' #输入文件的文件地址 output_file = r'C:\Users\aa\Desktop\22.xlsx' #输出文件的文件地址 第 4 节:创建和配置输出 Excel 文件

本节output_wb使用创建一个新的 Excel 工作簿openpyxl.Workbook()。它还设置output_sheet为用于写入邮政编码和位置信息的活动表。

output_wb = openpyxl.Workbook() output_sheet = output_wb.active 第 5 节:加载和处理输入 Excel 文件

此处,程序加载使用指定的输入 Excelinput_file文件openpyxl.load_workbook()。然后将其设置input_sheet为要从中读取数据的活动工作表。total_rows存储输入表中的总行数,该行数将用于进度条。

# Open the input sheet input_wb = openpyxl.load_workbook(input_file) input_sheet = input_wb.active # Get the total number of rows in the input sheet total_rows = input_sheet.max_row 第 6 节:处理输入表中的行并保存输出

在该部分中,程序使用迭代输入表的行input_sheet.iter_rows(values_only=True)。对于每一行,它检索邮政编码并调用该get_location函数来获取相应的位置信息。然后使用 将邮政编码和位置信息附加到输出表中output_sheet.append()。处理完所有行后,使用 `output_wb.save() 保存输出 Excel 文件。

# Iterate through the rows in the input sheet with a progress bar for row in tqdm(input_sheet.iter_rows(values_only=True), total=total_rows): postal_code = row[0] location = get_location(postal_code) # Write the postal code and location information to the output sheet output_sheet.append([postal_code, location]) # Save the output file output_wb.save(output_file) # Close the input and output files input_wb.close() output_wb.close() print("已经输出完毕!") 第 7 节:代码总结

代码根据邮政编码从网站检索位置信息,并将结果保存在 Excel 文件中。以下是其功能和优点的总结: 功能: 该程序通过使用提供的邮政编码向网站“ https://www.youbianku.com/ ”发送 HTTP GET 请求来获取位置信息,使用 XPath 从网站获得的 HTML 响应中提取位置信息。 该程序创建一个新的 Excel 文件并将邮政编码和相应的位置信息写入其中。 它提供了一个进度条,使用该tqdm库来跟踪输入 Excel 文件中行的处理情况。 程序处理未找到给定邮政编码的位置信息的情况。 优点: 自动位置检索:该程序自动执行根据邮政编码检索位置信息的过程,无需手动搜索。 高效处理:通过利用openpyxl库并使用 处理输入 Excel 文件中的行iter_rows(),程序可以高效地读取数据并将数据写入 Excel 文件。 进度跟踪:该tqdm库提供了一个进度条,使用户可以了解处理进度并估计剩余时间。 灵活性:该程序允许用户指定输入和输出文件路径,使其适应不同的文件位置。 错误处理:程序会妥善处理未找到邮政编码的位置信息的情况,从而防止潜在的错误或崩溃。 它可以使流程自动化、提高效率并提供进度跟踪,使其成为处理邮政编码数据的有用工具。

第 8 节:代码运行实例

输入数据:

输出数据(省市县是在一起输出的):

完整程序: import requests import openpyxl from lxml import etree from tqdm import tqdm def get_location(postal_code): url = f"https://www.youbianku.com/{postal_code}" response = requests.get(url) html = etree.HTML(response.text) location_element = html.xpath('/html/body/div[1]/h1') if location_element: return location_element[0].text else: return "无位置信息" # Load the Excel file input_file = r'C:\Users\aa\Desktop\11.xlsx' #输入文件的文件地址 output_file = r'C:\Users\aa\Desktop\22.xlsx' #输出文件的文件地址 # Create a new Excel file output_wb = openpyxl.Workbook() output_sheet = output_wb.active # Open the input sheet input_wb = openpyxl.load_workbook(input_file) input_sheet = input_wb.active # Get the total number of rows in the input sheet total_rows = input_sheet.max_row # Iterate through the rows in the input sheet with a progress bar for row in tqdm(input_sheet.iter_rows(values_only=True), total=total_rows): postal_code = row[0] location = get_location(postal_code) # Write the postal code and location information to the output sheet output_sheet.append([postal_code, location]) # Save the output file output_wb.save(output_file) # Close the input and output files input_wb.close() output_wb.close() print("已经输出完毕!")

 如有不足,欢迎留言指正交流哈!!!



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有