import re
import time
import requests
import pymysql
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
conn = pymysql.connect(host='localhost', user='root', passwd='789456', db='test', port=3306, charset='utf8')
cursor=conn.cursor()
url = 'https://tianqi.2345.com/'
html = requests.get(url).text
Pattern = re.compile('{"temp":(.*?)}')
datas = re.findall(Pattern, html)
url = 'https://tianqi.2345.com/'
service=Service('C:\Program Files\Google\Chrome\Application\chromedriver_win32\chromedriver.exe')
browser=webdriver.Chrome(service=service)
browser.get(url)
soup=BeautifulSoup(browser.page_source,'lxml')
data_quality=soup.find('div','banner-right-canvas-kq-i clearfix').find_all('i')
print('列表信息')
print(data_quality)
for num in data_quality:
quality=num.get_text()
for line in datas:
data = '"temp":' + line.encode('utf-8').decode('unicode_escape')
tmp = re.findall('"temp":"(.*?)"', data)
weather = re.findall('"weather":"(.*?)"', data)
day = re.findall('"day":"(.*?)"', data)
time = re.findall('"time_origin_text":"(.*?)"', data)
wind_direction = re.findall('"wind_direction":"(.*?)"', data)
wind_level = re.findall('"wind_level":"(.*?)"', data)
print(day[0], time[0], tmp[0] + '°', weather[0], wind_direction[0], wind_level[0], quality[0])
sql = "INSERT INTO mydata(day,tm,temp,weather,wind,wscale,quality) VALUES ('%s','%s','%s','%s','%s','%s','%s')" % (
day[0], time[0], tmp[0] + '°', weather[0], wind_direction[0], wind_level[0], quality[0])
cursor.execute(sql)
conn.commit()
conn.close()
|