【Python学习】爬虫:获取表格

python 复制代码
import requests
from bs4 import BeautifulSoup
headers={
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36'
}
place=[]
pm25=[]

def get_info(url):
    res=requests.get(url,headers=headers)
    soup=BeautifulSoup(res.text,'lxml')
    table = soup.find_all("table")
    rows = table[0].find_all("tr")

    i=0
    for row in rows:
        if(i>0):
            cols=row.find_all("td")
            place.append(cols[0].text)
            pm25.append(int(cols[1].text))

        i+=1

    for d1,d2 in zip(place,pm25):
        print(d1,d2)
        #print(d2)



url="http://www.pm25.in/nanjing"
get_info(url)
相关推荐
Fanxt_Ja1 天前
Langchain+Tavily对接大模型实现联网搜索
python·langchain·deepseek·tavily
一个心烑1 天前
【layui页面编辑下拉框处理的三种方式】
linux·python·layui
Ws_1 天前
C# 学习 Day1
开发语言·学习·c#
z200509301 天前
【linux学习】linux工具篇(下)
linux·学习
星夜夏空991 天前
STM32单片机学习(17) —— 串口外设中断
stm32·单片机·学习
魔法阵维护师1 天前
从零开发游戏需要学习的c#模块,第二十一章(精灵动画 —— 让角色走起来)
学习·游戏·c#
xingyuzhisuan1 天前
2026实测:租用RTX 4090 CUDA适配与PyTorch精准安装教程
人工智能·pytorch·python·深度学习·gpu算力
aP8PfmxS21 天前
网络爬虫是自动从互联网上采集数据的程序
爬虫
嗯、.1 天前
Agent 路由架构的一次尝试:LangGraph + Swarm Handoff + 小模型 Router
人工智能·python·swarm·langgraph·multi-agent·model-routing