爬虫爬取全国地名的中文和拼音

在做一些项目时需要用到全国地名的中文和拼音,因此我选取天气网站进行爬取。
结果是以两个列表的形式存放拼音和中文地名,有需要的可以自己进行修改扩充后使用。

# -*- coding: gbk -*-
import requests
from bs4 import BeautifulSoup
url ='https://lishi.tianqi.com'

def crwal(url):
    headers = {'User-Agent': 'Mozilla/5.0'}
    response = requests.get(url=url,  headers=headers,timeout=8)
    # response.raise_for_status()
    response.encoding = 'utf-8'
    page_text = response.text
    # print(page_text)

    bs = BeautifulSoup(page_text,"html.parser")
    test = []
    jiexi=bs.findAll('a')
    # print(jiexi)
    area_zwnameList=[]
    area_pinyinList=[]
    for i in jiexi:
        area_zwnameList.append(i.string)
        area_pinyinList.append(i['href'])
    # print(area_zwnameList)
    # print(area_pinyinList)
    # 输出地名拼音
    # print(area_pinyinList[77])
    # print(area_pinyinList[3430])
    # 输出地名中文
    # print(area_nameList[77])
    # print(area_nameList[3430])
    # 输出拼音+地名
    area_pinyinList_new = []
    area_zwnameList_new = []
    for i in range(77,3431):
        if int(len(area_zwnameList[i])) > 1 and int(len(area_pinyinList[i]) > 1):
            area_zwnameList_new.append(area_zwnameList[i])
            area_pinyinList_new.append(area_pinyinList[i].replace("/index.html","").replace("/",""))
    print(area_pinyinList_new)
    print(area_zwnameList_new)
crwal(url)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高哥的代码没Bug

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值