苏州网站开发公司有哪些,网络设计应该考虑的原则有哪些,网站建设制作及推广,外包经验会影响后续找工作吗前言#xff1a;在上一篇文章中#xff0c;我们介绍了在http://PM2.5.in这个网站采集空气质量的数据#xff0c;本篇文章是对其产生的一些问题的另一种解决方案#xff0c;提供更加权威的数据采集。技术框架#xff1a;selenium、json、etree这里的selenium是一种自动化测…前言在上一篇文章中我们介绍了在http://PM2.5.in这个网站采集空气质量的数据本篇文章是对其产生的一些问题的另一种解决方案提供更加权威的数据采集。技术框架selenium、json、etree这里的selenium是一种自动化测试的工具它可以帮助我们模拟浏览器打开网页并获取网页数据本文之所以选择这种方式进行是因为以requests方式直接请求无法获取到正确的数据这个网页的数据是动态加载需要用户执行点击操作才会被请求我们还是按照常规套路来分析下这个网站打开F12,看下这个网站的数据请求可以发现这个网站的数据的请求接口但当我们直接用requests去请求这个接口会发现无法获取正确的数据原因是这个网站采用了MmEwMD这个值进行了反爬虫这个是一个比较常见的反爬虫措施他这个值是在发起请求时动态生成的最简单的解决这个问题的办法就是采用selenium之类的模拟浏览器方法进行请求这样的话发出的请求也会自动带上这个参数请求的代码如下图所示driverPath browser\\chromedriver.exeoptions webdriver.ChromeOptions()options.add_experimental_option(excludeSwitches, [enable-automation])options.add_experimental_option(useAutomationExtension, False)# options.add_argument((--proxy-serverhttp:// ip))browser webdriver.Chrome(optionsoptions, executable_pathdriverPath)browser.execute_cdp_cmd(Page.addScriptToEvaluateOnNewDocument, {source: Object.defineProperty(navigator, webdriver, {get: () undefined})})browser.get(self.url)html browser.page_sourcebrowser.quit()# print(html)reponse etree.HTML(html)data reponse.xpath(//body/text())[0]json_data json.loads(data)我们通过调用谷歌浏览器直接请求对应的页面获取到数据后关闭浏览器通过etree解析网页结果通过观察发现我们获取到的数据是json数组因此我们使用json解析数据然后将对应的数据存储到数据库result_list json_data[data][hour]print(result_list)for result in result_list:item dict()item[affect] result[AFFECTINFO]item[action] result[SUGGEST]if(AQIPRIMPOLLUTE in result):item[primary_pollutant] result[AQIPRIMPOLLUTE]else:item[primary_pollutant] 无item[AQI] result[AQI]item[PM2.5/1h] result[PM25]item[PM10/1h] result[PM10]item[CO/1h] result[CO]item[NO2/1h] result[NO2]item[O3/1h] result[O3]item[O3/8h] result[O3_2]item[SO2/1h] result[SO2]item[city_name] result[POINTNAME]item[level] result[CODEAQILEVEL](result[AQILEVELNAME])item[live_data_time] result[MONITORTIME]item[live_data_time] datetime.datetime.strptime(item[live_data_time], %Y年%m月%d日%H)update_time item[live_data_time].strftime(%Y-%m-%d %H:%M:%S)item[live_data_unit] μg/m3(CO为mg/m3)if(item[city_name] in city_config):self.save_mysql(item)success_count success_count1log_text 采集的城市:{},采集的结果:{}.format(item[city_name],成功)self.save_log({log_type:0,log_text:log_text})self.save_log({log_type:3,log_text:log_text})self.update_spider_time(update_time)# 存储运行日志def save_log(self,item):sql INSERT INTO log(log_text,log_type,created_time) VALUES (%s,%s,%s)values [item[log_text],item[log_type],datetime.datetime.now()]self.cursor.execute(sql,values)self.conn.commit()def save_mysql(self,item):# 查询数据库已存在的数据query_sql select count(1) as count from kongqizhiliang where city_name %s and live_data_time %svalues [item[city_name],item[live_data_time]]self.cursor.execute(query_sql,values)data self.cursor.fetchone()# 如果不存在同一城市同一时刻更新的数据则新增if(data[count] 0):sql (INSERT kongqizhiliang(city_name,level,live_data_time,live_data_unit,AQI,PM25_1h,PM10_1h,CO_1h,NO2_1h,O3_1h,O3_8h,SO2_1h,affect,primary_pollutant,action) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s))values [item[city_name],item[level],item[live_data_time],item[live_data_unit],item[AQI],item[PM2.5/1h],item[PM10/1h],item[CO/1h],item[NO2/1h],item[O3/1h],item[O3/8h],item[SO2/1h],item[affect],item[primary_pollutant],item[action]]self.cursor.execute(sql,values)self.conn.commit()其实当初这个反爬虫措施也困扰了我一段时间的我这里采用的是最简单的方法解决虽然效率不高但能解决我的需求完整代码如下其中部分代码是可以不需要的必须redis和config那个你们自己改一下不会的可以问我这个是当时给别人毕设做的还有其他功能所以会有一些其他的采集空气质量的数据目标网站http://sthjt.hubei.gov.cn/hjsj/import requestsfrom lxml import etreeimport refrom xpinyin import Pinyinimport pymysqlimport sysfrom settings.config import *from utils import RedisUtilimport datetimeimport jsonfrom selenium import webdriverclass kongqizhiliang:DEFAULT_REQUEST_HEADERS {Accept: text/html,application/xhtmlxml,application/xml;q0.9,*/*;q0.8,Accept-Language: en,User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36}url http://sthjt.hubei.gov.cn/wcmapi/service/aqi.xhtmlredis_key kongqi:config_cityupdate_time kongqi:update_time# 汉字转拼音pinyin Pinyin()def __init__(self):self.conn pymysql.connect(hosthost, portport, useruser, passwdpasswd, dbdb, charsetcharset)self.cursor self.conn.cursor(cursorpymysql.cursors.DictCursor)# 将城市名转化为codedef get_code(self,city_name):return self.pinyin.get_pinyin(city_name, )def get_city_config(self):redis_util RedisUtil.get_redis()city_list redis_util.list_get_range(self.redis_key)return city_listdef update_spider_time(self,update_time):redis_util RedisUtil.get_redis()redis_util.str_set(self.update_time,update_time)def get_data(self):city_config self.get_city_config()log_text 采集开始,准备采集的城市:{},计划采集的数据量:{}.format(city_config,len(city_config))self.save_log({log_type:2,log_text:log_text})success_count 0update_time driverPath browser\\chromedriver.exeoptions webdriver.ChromeOptions()options.add_experimental_option(excludeSwitches, [enable-automation])options.add_experimental_option(useAutomationExtension, False)# options.add_argument((--proxy-serverhttp:// ip))browser webdriver.Chrome(optionsoptions, executable_pathdriverPath)browser.execute_cdp_cmd(Page.addScriptToEvaluateOnNewDocument, {source: Object.defineProperty(navigator, webdriver, {get: () undefined})})browser.get(self.url)html browser.page_sourcebrowser.quit()# print(html)reponse etree.HTML(html)data reponse.xpath(//body/text())[0]json_data json.loads(data)# print(json_data)result_list json_data[data][hour]print(result_list)for result in result_list:item dict()item[affect] result[AFFECTINFO]item[action] result[SUGGEST]if(AQIPRIMPOLLUTE in result):item[primary_pollutant] result[AQIPRIMPOLLUTE]else:item[primary_pollutant] 无item[AQI] result[AQI]item[PM2.5/1h] result[PM25]item[PM10/1h] result[PM10]item[CO/1h] result[CO]item[NO2/1h] result[NO2]item[O3/1h] result[O3]item[O3/8h] result[O3_2]item[SO2/1h] result[SO2]item[city_name] result[POINTNAME]item[level] result[CODEAQILEVEL](result[AQILEVELNAME])item[live_data_time] result[MONITORTIME]item[live_data_time] datetime.datetime.strptime(item[live_data_time], %Y年%m月%d日%H)update_time item[live_data_time].strftime(%Y-%m-%d %H:%M:%S)item[live_data_unit] μg/m3(CO为mg/m3)if(item[city_name] in city_config):self.save_mysql(item)success_count success_count1log_text 采集的城市:{},采集的结果:{}.format(item[city_name],成功)self.save_log({log_type:0,log_text:log_text})self.save_log({log_type:3,log_text:log_text})self.update_spider_time(update_time)# 存储运行日志def save_log(self,item):sql INSERT INTO log(log_text,log_type,created_time) VALUES (%s,%s,%s)values [item[log_text],item[log_type],datetime.datetime.now()]self.cursor.execute(sql,values)self.conn.commit()def save_mysql(self,item):# 查询数据库已存在的数据query_sql select count(1) as count from kongqizhiliang where city_name %s and live_data_time %svalues [item[city_name],item[live_data_time]]self.cursor.execute(query_sql,values)data self.cursor.fetchone()# 如果不存在同一城市同一时刻更新的数据则新增if(data[count] 0):sql (INSERT kongqizhiliang(city_name,level,live_data_time,live_data_unit,AQI,PM25_1h,PM10_1h,CO_1h,NO2_1h,O3_1h,O3_8h,SO2_1h,affect,primary_pollutant,action) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s))values [item[city_name],item[level],item[live_data_time],item[live_data_unit],item[AQI],item[PM2.5/1h],item[PM10/1h],item[CO/1h],item[NO2/1h],item[O3/1h],item[O3/8h],item[SO2/1h],item[affect],item[primary_pollutant],item[action]]self.cursor.execute(sql,values)self.conn.commit()if __name__ __main__:app kongqizhiliang()app.get_data()本文首发于爬虫利用selenium采集某某环境网站的空气质量数据www.bizhibihui.com