python一行代码开启http

 

  python -m SimpleHTTPServer 8000 &

  监听8000端口 浏览器用127.0.0.1:8000访问

  如果出现no module named SimpleHTTPServer

  使用 python -m http.server 8000 &

 

转载于:https://www.cnblogs.com/rookie404/p/6128959.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,我们需要准备好以下工具: - requests 库,用于发送 HTTP 请求和获取响应; - BeautifulSoup 库,用于解析 HTML 页面; - pandas 库,用于处理数据和生成表格; - matplotlib 库,用于绘制图表。 接下来,我们可以按照以下步骤爬取北京地铁客流量: 1. 打开北京市地铁客流量查询页面(http://www.bjsubway.com/e/action/ListInfo/?classid=39),发起 GET 请求,并获取响应内容。 ```python import requests url = "http://www.bjsubway.com/e/action/ListInfo/?classid=39" response = requests.get(url) html = response.content ``` 2. 使用 BeautifulSoup 库解析 HTML 页面,并找到包含客流量数据的表格。 ```python from bs4 import BeautifulSoup soup = BeautifulSoup(html, "html.parser") table = soup.find("table", class_="table01") ``` 3. 遍历表格中的每一行,提取每个站点的客流量数据,并将其保存到一个字典中。 ```python data = {} for tr in table.find_all("tr")[1:]: td_list = tr.find_all("td") station = td_list[1].text.strip() in_count = int(td_list[2].text.strip()) out_count = int(td_list[3].text.strip()) total_count = in_count + out_count data[station] = { "in_count": in_count, "out_count": out_count, "total_count": total_count } ``` 4. 将数据保存到一个 CSV 文件中,并使用 pandas 库读取该文件并生成一个数据框。 ```python import csv import pandas as pd with open("beijing_subway.csv", "w", newline="", encoding="utf-8") as f: writer = csv.writer(f) writer.writerow(["station", "in_count", "out_count", "total_count"]) for station, count_dict in data.items(): writer.writerow([station, count_dict["in_count"], count_dict["out_count"], count_dict["total_count"]]) df = pd.read_csv("beijing_subway.csv") ``` 5. 利用数据框生成图表,展示各个站点的客流量情况。 ```python import matplotlib.pyplot as plt import matplotlib.font_manager as fm # 设置中文字体 font_path = "C:\Windows\Fonts\SimHei.ttf" font_prop = fm.FontProperties(fname=font_path, size=14) # 绘制柱状图 plt.figure(figsize=(12, 6)) plt.bar(df.station, df.total_count) plt.title("北京市地铁客流量情况", fontproperties=font_prop) plt.xlabel("站点", fontproperties=font_prop) plt.ylabel("客流量(人次)", fontproperties=font_prop) plt.xticks(rotation=90, fontproperties=font_prop) plt.grid(axis="y") plt.show() ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值