python-爬虫-三字代码网站爬取

三字代码 http://www.6qt.net/

在这里插入图片描述

爬取城市、三字代码、所属国家、国家代码、四字代码、机场名称、英文名称、查询次数

import requests

url = 'http://www.6qt.net/'
r = requests.get(url)
r.encoding='gb2312'
print(r.text)

在这里插入图片描述
使用xpath解析,得到城市名

html.fromstring(html, base_url=None, parser=None, **kw)
Parse the html, returning a single element/document.解析html,返回单个元素/文档

import requests
from lxml import html

url = 'http://www.6qt.net/'
r = requests.get(url)
r.encoding='gb2312'
data_html = html.fromstring(r.text)
# 提取所有class属性为tdbg的tr元素
tr_html = data_html.xpath('//tr[@class="tdbg"]')# 得到Element对象列表# <Element tr at 0x20695cfdd18>
for tr in tr_html:
    city_name = tr.xpath('td[1]/a/text()')
    if city_name:
        print(city_name[0])

在这里插入图片描述

完整代码

import requests
from lxml import html

url = 'http://www.6qt.net/'
r = requests.get(url)
r.encoding='gb2312'
data_html = html.fromstring(r.text)
tr_html = data_html.xpath('//tr[@class="tdbg"]')
for tr in tr_html:
    data = {}
    city_name = tr.xpath('td[1]/a/text()')
    if city_name:
        data['city_name'] = city_name[0].replace('\xa0','')
    tcc = tr.xpath('td[2]/a/text()')	# Three character code三字代码
    if city_name:
        data['tcc'] = tcc[0].replace('\xa0','')
    country = tr.xpath('td[3]/a/u/text()')
    if city_name:
        data['country'] = country[0]
    country_code = tr.xpath('td[4]/a/u/text()')
    if city_name:
        data['country_code'] = country_code[0]
    fcc = tr.xpath('td[5]/a/u/text()')
    if city_name:
        if fcc:		# 有的城市没有四字代码
            data['fcc'] = fcc[0]
        else:
            data['fcc'] = ''	# 没有四字代码用空字符串代替
    airport_name = tr.xpath('td[6]/text()')
    if city_name:
        data['airport_name'] = airport_name[0].replace('\xa0','')
    en_name = tr.xpath('td[7]/text()')
    if city_name:
        data['en_name'] = en_name[0].replace('\xa0','')
    number = tr.xpath('td[8]/a/text()')
    if city_name:
        data['number'] = number[0]
    
    if data:
        print(data)

在这里插入图片描述

ASCII 字符集中的\xa0代表的是非打印字符"非断行空格",也称为"不间断空格",在网页中通常用于表示空格或保持文本的格式。在 HTML 中,它被称为“&nbsp;”实体。


可以使用lxmltostring()函数将一个Element对象转换成可读的代码。例如:

from lxml import etree

# 创建一个HTML文档的根元素
root = etree.Element("html")

# 创建一个<head>标签并添加到根元素中
head = etree.SubElement(root, "head")

# 创建一个<title>标签并添加到<head>标签中
title = etree.SubElement(head, "title")
title.text = "Welcome to my website"

# 将根元素转换为可读的HTML代码并打印输出
html_code = etree.tostring(root, pretty_print=True, encoding='unicode')
print(html_code)

运行上述代码后,输出的结果为:

<html>
  <head>
    <title>Welcome to my website</title>
  </head>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

蔷莫

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

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

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

打赏作者

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

抵扣说明:

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

余额充值