大学生求职创业补贴_网站建设制作设计_网络设计培训班_电子商务交易平台

网络工程规划与设计

dw网页设计与制作、如何申请个人网站、永久使用、不限域名、青岛建设工程信息网官网

临沂专业网站建设公司?

大学生求职创业补贴_网站建设制作设计_网络设计培训班_电子商务交易平台

# 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 互联网做什么比较赚钱免费创建社区平台怎样设计一个logo微信小程序商城制作两人世界高清完整版免费观看仿网站建设湖南定制响应式网站有哪些怎么开通个人网站招聘平台人武部正规化建设app软件开发流程微信小程序制作视频教程做网站方案百度推广开户需要多少钱猎头公司是做什么的怎么注册一个电商平台高端网站建设哪些好重庆企业建站程序公司logo设计图片大全公司网络桂林生活网app下载软件定制开发多少钱公司网站页面设计图片qq刷网站空间建站怎么建国家政务服务平台官网网络优化工程师骗局全网网站快速排名推广软件企业管理生产管理系统公司局域网搭建网页设计理念和设计构思

猜你喜欢

  • 友情链接:
  • 湖南网站推广公司 在线制作简历的网站 b2b商业模式 seo顾问张智伟 手机微信网站建设 商城app下载安装