北京疫情大爆发是真的吗_免费海外网站服务器九幺_免费建站申请_桂林漓江和阳朔是一个地方吗 # sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8') #改变标准输出的默认编码class mysql:def __init__(self):self.connect = pymysql.connect(host='localhost',port=3306,user='root',password='x20000317',database='web_work',charset='utf8mb4')# 以字典的形式获取查询结果self.cursor = self.connect.cursor(pymysql.cursors.DictCursor)def save(self,*args):passdef read(self,*args):passdef exit(self):self.cursor.close()self.connect.close()class weather(mysql):def __init__(self):super().__init__()self.headers = {'User-Agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Mobile Safari/537.36 Edg/95.0.1020.53'}# 定义一个列表存储所有详情页urlself.url_list = []# 八个地区名称列表,多页爬取self.area_list = ['hb','db','hd','hz','hn','xb','xn','gat']# 存储一周内综合天气数据self.weekdata = []# 存储一天内各小时数据self.hourdata = []# 创建数据库sql_1 = 'create table week(id int primary key auto_increment,city varchar(20),date char(20),weather varchar(20),max_tem char(10),min_tem char(10),wind char(10))'sql_2 = 'create table hour(id int primary key auto_increment,city char(15),time char(4),temperature int,humidity int,wind_direction varchar(10),wind_power int)'try:self.cursor.execute(sql_1)except:passtry:self.cursor.execute(sql_2)except:pass# 获取详情页网址def get_url(self):for area_name in self.area_list:# 中国天气预报官网urlmain_url = 'http://www.weather.com.cn/textFC/{}.shtml'.format(area_name)# 获取页面html内容并用etree解析page_text = requests.get(url=main_url, headers=self.headers).texttree = etree.HTML(page_text)td_list = tree.xpath("//div[@class='hanml']/div[1]//td[@class='last']")for td in td_list:new_url = td.xpath("./a/@href")if new_url and (new_url[0] not in self.url_list):self.url_list.append(new_url[0])# 正则表达式获取数据def v_func(self, string):value_list = []time_pattern = '"od21":"(.*?)",'# 字符串列表time_list = re.findall(time_pattern, string, re.M)temperature_pattern = '"od22":"(.*?)",'# 字符串列表temperature_list = re.findall(temperature_pattern, string, re.M)humidity_pattern = '"od27":"(.*?)",'# 字符串列表humidity_list = re.findall(humidity_pattern, string, re.M)wind_pattern = '"od24":"(.*?)","od25":"(.*?)",'# 字符串元组列表wind_list = re.findall(wind_pattern, string, re.M)for i in range(len(time_list)):value_dict = {'time': time_list[i],'temperature': temperature_list[i],'humidity': humidity_list[i],'wind_direction': wind_list[i][0],'wind_power': wind_list[i][1]}value_list.insert(0, value_dict)return value_list# 获取详情页数据def get_data(self):self.get_url()for url in self.url_list:page = requests.get(url=url, headers=self.headers)page.encoding="utf-8"tree = etree.HTML(page.text)# 城市名信息处理city_list1 = tree.xpath("//div[@class='crumbs fl']//text()")city_list2 = [item.strip('全国').strip('>') for item in city_list1]city_list3 = [item.strip() for item in city_list2 if item.strip() != '']self.city_info = '-'.join(city_list3)# 城市一周天气数据处理li_list = tree.xpath("//ul[@class='t clearfix']/li")for li in li_list:text_list1 = li.xpath(".//text()")# 去除/text_list2 = [item.strip('/') for item in text_list1]# 去除\ntext_list3 = [item.strip() for item in text_list2 if item.strip()!='']text_list3.insert(0,self.city_info)if len(text_list3)!=6:text_list3.insert(3,'None')self.weekdata.append(text_list3)# 城市小时数据处理string = tree.xpath("//div[@class='left-div']/script//text()")[0]data_dict = {'name':re.findall('"od1":"(.*?)",',string)[0],'value':self.v_func(string)}self.hourdata.append(data_dict)self.save(self.weekdata,1)self.save(self.hourdata,2)# 重写保存方法def save(self,data,num):if num == 1:sql = 'truncate table week'self.cursor.execute(sql)self.connect.commit()sql = 'insert into week(city,date,weather,max_tem,min_tem,wind) values(%s,%s,%s,%s,%s,%s)'for item in data:self.cursor.execute(sql,(item[0],item[1],item[2],item[3],item[4],item[5]))self.connect.commit()elif num == 2:sql = 'truncate table hour'self.cursor.execute(sql)self.connect.commit()sql = 'insert into hour(city,time,wind_direction,temperature,humidity,wind_power) values(%s,%s,%s,%s,%s,%s)'for data_item in data:for item in data_item['value']:try:self.cursor.execute(sql,(data_item['name'],item['time'],item['wind_direction'],item['temperature'],item['humidity'],item['wind_power']))except Exception as e:print(e)print(data_item['name'],item)self.connect.commit()# 重写读取方法def read(self,num,data):if num == 1:sql = 'select * from week where city LIKE %s'self.cursor.execute(sql,('%'+data+'%'))elif num == 2:sql = 'select * from hour where city=%s'self.cursor.execute(sql,(data))# 当数据库内没有数据时返回的是元组,需要转换为列表info_list = list(self.cursor.fetchall())# print(info_list)return info_list">

手机兼职打字员

网络公司代理合同、关键词点击排名软件、永久使用、不限域名、公司网络建设管理措施怎么写

电商前沿资讯?

北京疫情大爆发是真的吗_免费海外网站服务器九幺_免费建站申请_桂林漓江和阳朔是一个地方吗

2.8.2 Tomcat的特点

网络推广软文案例建立平台的费用有多少e福州app下载画册设计品牌网络营销商业策划书东莞横沥镇怎么开发软件视频教程海外网站代购直播开放平台官网入口特效素材库沧州网站推广博兴网站设计公司b2b2c名词解释站长之家收录查域名网站真实ip制作简历网页微信开发用什么平台好营销网站建设规划方案个人域名备案可以做什么网站中国中铁最新消息网站建设客服中心广州关键词快速排名印花图案原创设计网站广州正佳广场地铁站在线客服系统 开源网推所的案源哪来的免费软件正能量全世界疫情最新情况深圳东门老街美食行情免费软件大全app下载凡客诚品的发展历程

猜你喜欢

  • 友情链接:
  • 怎么从网上销售自家产品 全球十大互联网公司排名 外贸网站建设网站排名优化 网站优化及推广 王爷到小红 山东唯一没有疫情的城市