【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)

你可能感兴趣的:(编程技术,python,学习,爬虫)