一、爬取目标
二、完整源码
#""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
#
# Copyright (c) 2024 愤怒的it男, All Rights Reserved.
# FileName : code.py
# Date : 2024.01.29
# Author : 愤怒的it男
# Version : 1.0.0
# Node : 欢迎关注微信公众号【愤怒的it男】
#
#""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
from lxml import etree
import csv
from prettytable import PrettyTable
from playwright.sync_api import sync_playwright
def getData():
with sync_playwright() as p:
browser_type = p.chromium
browser = browser_type.launch()
page = browser.new_page(ignore_https_errors=True)
page.goto('https://data.stats.gov.cn/easyquery.htm')
page.get_by_text("交通运输").click()
page.get_by_text("旅客运输量").click()
page.locator("//div[@class='dtHead']").click()
page.get_by_text("最近36个月").click()
html = etree.HTML(page.content())
thead = html.xpath("//table[@class='public_table table_column']/thead/tr/th/strong/text()")
tbody = []
trs = html.xpath("//table[@class='public_table table_column']/tbody/tr")
for tr in trs:
row = []
index1 = tr.xpath("td[1]/a/following::text()[1]")[0]
index2 = tr.xpath("td[1]/a/following::text()[2]")[0]
index = str(index1) + '|' + str(index2)
row.append(index)
row = row + tr.xpath("td[@align='right']/text()")
tbody.append(row)
browser.close()
return (thead, tbody)
def printData(thead, tbody):
table = PrettyTable()
table.add_column("时间",thead[1:])
for index,row in enumerate(tbody):
if index in [0,4,8,12,16]:
table.add_column(row[0],row[1:])
print(table)
#table.add_column("时间",thead[1:])
def saveData(thead, tbody):
with open('旅客运输量.csv', 'w', encoding='utf-8', newline='') as file:
writer = csv.writer(file)
writer.writerow(thead)
writer.writerows(tbody)
def main():
print('数据采集中,请耐心等待……')
result = getData()
printData(result[0], result[1])
saveData(result[0], result[1])
if __name__== "__main__" :
main()
三、爬取结果
+------------+---------------------+-------------------------+-------------------------+-------------------------+-------------------------+
| 时间 | 客运量|当期值(万人) | 铁路客运量|当期值(万人) | 公路客运量|当期值(万人) | 水运客运量|当期值(万人) | 民航客运量|当期值(万人) |
+------------+---------------------+-------------------------+-------------------------+-------------------------+-------------------------+
| 2023年12月 | 73965 | 28986 | 38640 | 1280 | 5059 |
| 2023年11月 | 74495 | 27974 | 39967 | 1655 | 4899 |
| 2023年10月 | 85182 | 35169 | 41888 | 2520 | 5605 |
| 2023年9月 | 79730 | 32499 | 39980 | 1902 | 5349 |
| 2023年8月 | 94474 | 42860 | 41625 | 3593 | 6396 |
| 2023年7月 | 90993 | 40919 | 40798 | 3034 | 6243 |
| 2023年6月 | 79073 | 32682 | 39041 | 2039 | 5312 |
| 2023年5月 | 79323 | 32560 | 39141 | 2453 | 5170 |
| 2023年4月 | 78400 | 32900 | 38300 | 2200 | 5000 |
| 2023年3月 | 70595 | 28038 | 36127 | 1861 | 4569 |
| 2023年2月 | 65872 | 27297 | 32551 | 1704 | 4320 |
| 2023年1月 | 58327 | 23609 | 29187 | 1554 | 3977 |
| 2022年12月 | 36123 | 9913 | 23771 | 568 | 1871 |
| 2022年11月 | 33481 | 7374 | 24146 | 704 | 1257 |
| 2022年10月 | 42794 | 11896 | 28147 | 1159 | 1592 |
| 2022年9月 | 48413 | 14752 | 30700 | 950 | 2010 |
| 2022年8月 | 59623 | 21732 | 32890 | 1770 | 3230 |
| 2022年7月 | 61900 | 22615 | 34184 | 1701 | 3400 |
| 2022年6月 | 51605 | 16736 | 31637 | 1032 | 2200 |
| 2022年5月 | 37782 | 9225 | 26686 | 664 | 1207 |
| 2022年4月 | 32506 | 5691 | 25595 | 432 | 788 |
| 2022年3月 | 42108 | 10145 | 29759 | 667 | 1537 |
| 2022年2月 | 56582 | 19052 | 33328 | 1073 | 3129 |
| 2022年1月 | 55534 | 17874 | 33801 | 908 | 2951 |
| 2021年12月 | 57633 | 16498 | 37587 | 839 | 2710 |
| 2021年11月 | 54039 | 14396 | 36521 | 969 | 2153 |
| 2021年10月 | 72751 | 24767 | 42429 | 1669 | 3886 |
| 2021年9月 | 67930 | 22711 | 40505 | 1102 | 3612 |
| 2021年8月 | 54904 | 15409 | 36243 | 1012 | 2241 |
| 2021年7月 | 83398 | 30847 | 45695 | 1944 | 4912 |
| 2021年6月 | 75138 | 24607 | 44895 | 1507 | 4128 |
| 2021年5月 | 84868 | 29415 | 48202 | 2146 | 5104 |
| 2021年4月 | 80639 | 28110 | 45605 | 1814 | 5109 |
| 2021年3月 | 74544 | 24376 | 44087 | 1299 | 4782 |
| 2021年2月 | 57293 | 14318 | 39397 | 1184 | 2395 |
| 2021年1月 | 67112 | 15715 | 47527 | 852 | 3018 |
+------------+---------------------+-------------------------+-------------------------+-------------------------+-------------------------+
四、免责声明
本文所涉及源代码均为个人学习所编写,并于公众号(愤怒的it男)中原创发布,仅可用于计算机技术学习及研究等合法行为,禁止利用本文所涉及源代码从事任何违反本国(地区)法律法规的业务,如有发现存在违法违规行为我会举报到网监部门。
原文始发于微信公众号(愤怒的it男):中国春运!Python爬取中国近3年来的旅客运输量
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/234395.html