使用Python 爬取歷史天氣

2021-10-10 16:00:15 字數 1373 閱讀 2965

最近做個簡單小專案需要一些天氣資料值,所以想去網上爬取一些。下面是我的爬取案例。使用的**是2345天氣網。

2.xpath表示式:

3.取文字

import requests

from lxml import etree

defget_table

(url)

: headers =

# 請求資料 並獲得返回的json資料

text_json = requests.get(url=url, headers=headers)

.json(

) etree_html = etree.html(text_json[

'data'])

# 利用xpath獲得所有tr標籤

html_xpath = etree_html.xpath(

'.//tr')[

1:] data =

for ls in html_xpath:

# tr標籤的所有值

xpath = ls.xpath(

'.//text()'

)# 進行簡單資料處理

xpath =

list

(map

(lambda x: x.strip(

), xpath)

) xpath =

[x for x in xpath if

len(x)!=0

]return data

if __name__ ==

'__main__'

: url =

''# 將2012 年到2023年 1- 12 月的所有資料儲存到本地的csv檔案

python爬取歷史天氣資料

import requests from requests.exceptions import requestexception from bs4 import beautifulsoup import os import csv import time def get one page url 獲...

Python 爬蟲,爬取歷史天氣資料

先上原始碼 這次用的是beautifulsoup,解析html,非常的便捷 import datetime import pandas as pd import re import requests import time from bs4 import beautifulsoup headers ...

python 爬取天氣

準備工作做好了,接下來就是 了 用py爬天氣資訊,需要使用兩個模組,分別是urllib2 獲取資料 和json 解析資料 coding utf 8 import urllib2 import json from city import city cityname raw input 你想查哪個城市的...