非常抱歉,下面是修改后的代码,修正了格式和添加了代码说明:
# -*- coding:utf-8 -*-
import requests
from bs4 import BeautifulSoup
from openpyxl import Workbook
# 根据地址获取页面内容,并返回BeautifulSoup
def get_html(url):
# 若页面打开失败,则无限重试,没有后退可言
while True:
try:
# 超时时间为1秒
response = requests.get(url, timeout=1)
response.encoding = "GBK"
if response.status_code == 200:
return BeautifulSoup(response.text, "lxml")
else:
continue
except Exception:
continue
# 获取地址前缀(用于相对地址)
def get_prefix(url):
return url
# 递归抓取下一页面
def spider_next(url, lev, sheet):
if lev == 2:
spider_class = "city"
elif lev == 3:
spider_class = "county"
elif lev == 4:
spider_class = "town"
else:
spider_class = "village"
for item in get_html(url).select("tr." + spider_class + "tr"):
item_td = item.select("td")
item_td_code = item_td.select_one("a")
item_td_name = item_td.select_one("a")
if item_td_code is None:
item_href = None
item_code = item_td.text
item_name = item_td.text
if lev == 5:
item_name = item_td.text
else:
item_href = item_td_code.get("href")
item_code = item_td_code.text
item_name = item_td_name.text
# 输出:级别、区划代码、名称
content2 = f"{lev}\t{item_code}\t{item_name}"
print(content2)
sheet.append([lev, item_code, item_name])
if item_href is not None:
spider_next(get_prefix(url) + item_href, lev + 1, sheet)
if __name__ == '__main__':
province_url = "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2020/index.html"
province_list = get_html(province_url).select('tr.provincetr a')
wb = Workbook()
sheet = wb.active
sheet.title = "行政区划"
# 写入表头
sheet.append(["级别", "区划代码", "名称"])
for index, province in enumerate(province_list, start=2):
href = province.get("href")
province_code = href + "0000000000"
province_name = province.text
content = f"1\t{province_code}\t{province_name}"
print(content)
sheet.append([1, province_code, province_name])
spider_next(get_prefix(province_url) + href, 2, sheet)
# 保存xlsx文件
wb.save(r'E:\Temp\区划代码.xlsx')
这次修正后的代码使用了更符合PEP8规范的代码格式,同时添加了注释来解释每个代码段的功能。希望这次能满足你的要求!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |