參考文章:
使用python爬取全國天氣數(shù)據(jù)并導(dǎo)入MySQL數(shù)據(jù)庫表
python-高級篇 在pycharm中對python項目進行打包操作
完整代碼:
# 導(dǎo)入模塊
import datetime
from bs4 import BeautifulSoup
import requests
import pymysql
# 打開數(shù)據(jù)庫連接,并使用cursor()建立一個游標(biāo)對象
conn = pymysql.connect(host='localhost', user='root', passwd='root', db='mysql', port=3306, charset='utf8')
cursor = conn.cursor()
# 創(chuàng)建request對象尼摹,指定url和請求頭(user-agent),目的是為了更真實的模擬瀏覽器
def get_temperature(url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
} # 設(shè)置頭文件信息
response = requests.get(url, headers=headers).content # 提交requests.get請求痹届,傳遞url和headers
soup = BeautifulSoup(response, "lxml") # 用Beautifulsoup 進行解析
conmid = soup.find('div', class_='conMidtab')
conmid2 = conmid.findAll('div', class_='conMidtab3')
for info in conmid2:
tr_list = info.find_all('tr')[0:] # 使用切片取到第三個tr標(biāo)簽
for index, tr in enumerate(tr_list): # enumerate可以返回元素的位置及內(nèi)容
td_list = tr.find_all('td')
if index == 0:
province_name = td_list[0].text.replace('\n', '') # 取每個標(biāo)簽的text信息峦嗤,并使用replace()函數(shù)將換行符刪除
city_name = td_list[1].text.replace('\n', '')
weather = td_list[5].text.replace('\n', '')
wind = td_list[6].text.replace('\n', '')
max = td_list[4].text.replace('\n', '')
min = td_list[7].text.replace('\n', '')
date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print(province_name)
else:
city_name = td_list[0].text.replace('\n', '')
weather = td_list[1].text.replace('\n', '')
wind = td_list[2].text.replace('\n', '')
max = td_list[3].text.replace('\n', '')
min = td_list[6].text.replace('\n', '')
date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print(city_name, weather, wind, max, min, date)
if city_name == '江陰' or city_name == '蘇州':
sql = "INSERT INTO tqyb(city, weather, wind, max, min,date) VALUES ('%s', '%s', '%s', '%s', '%s', '%s')" % (
city_name, weather, wind, max, min, date)
cursor.execute(sql)
if __name__ == '__main__':
urls = ['http://www.weather.com.cn/textFC/jiangsu.shtml']
for url in urls:
get_temperature(url)
conn.commit()
在Terminal下輸入:“PyInstaller -F -w *.py” 就可以制作出exe躬审。生成的文件放在同目錄dist下杉辙。
pyinstaller -F weather.py