营销型网站建设郑州_宣传网站制作流程_个人企业邮箱_济南莱芜区疫情最新

网页怎么升级

倍速课堂小程序入口、长春火车站咨询处电话、永久使用、不限域名、网络维修公司

企查查企业查询在线?

营销型网站建设郑州_宣传网站制作流程_个人企业邮箱_济南莱芜区疫情最新

# 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 企业财务报表查询网站空间网架结构创建自己的视频号杭州注册公司流程和费用重庆网站运营商常见c2c电商平台有哪些国家投资开发公司官网优秀网站设计师采招网招标公告626969域名查询网请问本地建站网址多少展示厅设计图片大全鞍山微信小程序制作永兴网站定制中国十大舆情网络平台培训网站建设网精准ip地址查询工具高效的网站建设明细报价表企业名录网站泉州百讯网络科技有限公司万能浏览器官方下载学编程培训班深圳专业网站设计开发网络安全有名的培训学校潍坊网站建设方案开发如何制作平台软件webflow网页制作东莞网络运营公司中华人民共和国城乡建设官网网络运营零基础教学网页传奇脚本

猜你喜欢

  • 友情链接:
  • 怎么开一家公司 西宁网络科技有限公司 网站服务器有什么用 四川建设网官方 自媒体引流推广 2345网址入口