时间:2023-07-21 07:24:01 | 来源:网站运营
时间:2023-07-21 07:24:01 来源:网站运营
Python采集12星座信息,分析出12星座的各个特点:import reimport queueimport requestsimport threadingfrom selenium import webdriverfrom selenium.webdriver.chrome.options import Options headers = {'Host': 'www.douban.com','Connection': 'keep-alive','Cache-Control': 'max-age=0','Upgrade-Insecure-Requests': '1','User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36','Sec-Fetch-Dest': 'document','Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9','Sec-Fetch-Site': 'none','Sec-Fetch-Mode': 'navigate','Sec-Fetch-User': '?1','Accept-Encoding': 'gzip, deflate, br','Accept-Language': 'zh-CN,zh;q=0.9','Cookie': 'bid=rb_kUqiDS6k; douban-fav-remind=1; _pk_ses.100001.8cb4=*; ap_v=0,6.0; __utma=30149280.1787149566.1585488263.1585488263.1585488263.1; __utmc=30149280; __utmz=30149280.1585488263.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __yadk_uid=HNoH1YVIvD2c8HrQDWHRzyLciFJl1AVD; __gads=ID=a1f73d5d4aa31261:T=1585488663:S=ALNI_MafqKPZWHx0TGWTpKEm8TTvdC-eyQ; ct=y; _pk_id.100001.8cb4=722e0554d0127ce7.1585488261.1.1585488766.1585488261.; __utmb=30149280.10.6.1585488263'} # driver初始化chrome_options = Options()chrome_options.add_argument('--headless')driver = webdriver.Chrome(options=chrome_options) # 下载图片def downimg(): while not img_queue.empty(): img = img_queue.get() img_name = img[0] url = img[1] res = requests.get(url) data =res.content with open('./img/%s.webp'%img_name,'wb') as f: f.write(data) print(img_name) # 网站参数url_o = 'https://www.douban.com/photos/album/1872547715/?m_start=%d' # 爬取连接img_queue = queue.Queue()for i in range(0,21): url = url_o%(18*i) driver.get(url) es = driver.find_elements_by_class_name('photo_wrap') for e in es: img_e = e.find_element_by_tag_name('img') img_url = img_e.get_attribute('src') img_url = img_url.replace('photo/m/public','photo/l/public') # 替换为大图 text_e = e.find_element_by_class_name('pl') img_date = text_e.text img_queue.put((img_date,img_url)) print('%d页爬取完成'%(i+1))driver.close() # 下载图片 thread_list = []N_thread = 5for i in range(N_thread): thread_list.append(threading.Thread(target=downimg))for t in thread_list: t.start()for t in thread_list: t.join()
代码简单来说就是:webdriver访问页面并获取图片地址,然后通过多线程利用requests下载并保存图片。# 读取数据data = pd.read_csv('./analyze/20200330-20200330.csv',encoding='utf-8') # 筛选数据(和NASA相关且有有效日期的数据)data_NASA = []for i in range(len(data)): url = urllib.parse.unquote(data['URL'][i]) pv = data['PV'][i] # 浏览量 uv = data['UV'][i] # 访客量 #if url[-1] == '日' and 'NaN' not in url: # 为NASA访问页面 if 'date=' in url and 'NaN' not in url: try: data_NASA.append((re.findall('date=(/d*?月/d*?日)',url)[0],pv,uv)) except: pass
# 统计各个天数的频率PV_map= {}UV_map = {}PV_total = 0UV_total = 0for d in data_NASA: if d[0] not in PV_map.keys(): PV_map[d[0]] = 0 UV_map[d[0]] = 0 PV_map[d[0]] += d[1] # PV UV_map[d[0]] += d[2] # UV PV_total += d[1] UV_total += d[2]for k in PV_map.keys(): # 计算频率 PV_map[k] = PV_map[k]/PV_total*100 UV_map[k] = UV_map[k]/UV_total*100PVs= sorted(PV_map.items(),key=lambda x:x[1],reverse=True) # 排序UVs= sorted(UV_map.items(),key=lambda x:x[1],reverse=True) # 排序
# 判断星座def get_xingzuo(month, date): dates = (21, 20, 21, 21, 22, 22, 23, 24, 24, 24, 23, 22) constellations = ("摩羯座", "水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座") if date < dates[month-1]: return constellations[month-1] else: return constellations[month] # 统计各星座的频率xingzuo = ("摩羯座", "水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座")xingzuo_map = {}for x in xingzuo: xingzuo_map[x] = 0xingzuo_total = 0for d in data_NASA: month = int(re.findall('(/d*?)月(/d*?)日',d[0])[0][0]) day = int(re.findall('(/d*?)月(/d*?)日',d[0])[0][1]) x = get_xingzuo(month,day) #xingzuo_map[x] += d[1] # PV xingzuo_map[x] += d[2] # UV xingzuo_total += d[2]for k in xingzuo_map.keys(): xingzuo_map[k] = xingzuo_map[k]/xingzuo_total*100xingzuos= sorted(xingzuo_map.items(),key=lambda x:x[1],reverse=True) # 排序
# 统计各月份的频率month = [str(i)+'月' for i in range(1,13)]month_map = {}for m in month: month_map[m] = 0month_total = 0for d in data_NASA: m = d[0].split('月')[0]+'月' #month_map[m] += d[1] # PV month_map[m] += d[2] # UV month_total += d[2]for k in month_map.keys(): month_map[k] = month_map[k]/month_total*100months= sorted(month_map.items(),key=lambda x:x[1],reverse=True) # 排序
## 生日查询TOP10-按访客量UVdate = []uv = []for i in UVs: date.append(i[0]) uv.append(i[1])top10_date = date[:10]top10_date.reverse()top10_uv = uv[:10]top10_uv.reverse()fig, ax = plt.subplots() # 画图b = plt.barh(top10_date,top10_uv,color='#6699CC') # 金色#FFFACD 银色#C0C0C0 橙色#FFA500 蓝色#6699CCi = len(b)for rect in b: # 画数值 if i==3: # 第三名 rect.set_facecolor('#FFA500') # 橙色 if i==2: # 第二名 rect.set_facecolor('#C0C0C0') # 银色 if i==1: # 第一名 rect.set_facecolor('#FFFACD') # 金色 w = rect.get_width() ax.text(w, rect.get_y()+rect.get_height()/2, ' %.2f%%'%w,ha='left', va='center') i -= 1plt.xticks([]) # 关掉横坐标 ## 星座查询排名 name = []v = []for i in xingzuos: name.append(i[0]) v.append(i[1])name.reverse()v.reverse()fig, ax = plt.subplots() # 画图b = plt.barh(name,v,color='#6699CC') # 金色#FFFACD 银色#C0C0C0 橙色#FFA500 蓝色#6699CCi = len(b)for rect in b: # 画数值 if i==3: # 第三名 rect.set_facecolor('#FFA500') # 橙色 if i==2: # 第二名 rect.set_facecolor('#C0C0C0') # 银色 if i==1: # 第一名 rect.set_facecolor('#FFFACD') # 金色 w = rect.get_width() ax.text(w, rect.get_y()+rect.get_height()/2, ' %.2f%%'%w,ha='left', va='center') i -= 1plt.xticks([]) # 关掉横坐标 ## 月份查询排名 name = []v = []for i in months: name.append(i[0]) v.append(i[1])name.reverse()v.reverse()fig, ax = plt.subplots() # 画图b = plt.barh(name,v,color='#6699CC') # 金色#FFFACD 银色#C0C0C0 橙色#FFA500 蓝色#6699CCi = len(b)for rect in b: # 画数值 if i==3: # 第三名 rect.set_facecolor('#FFA500') # 橙色 if i==2: # 第二名 rect.set_facecolor('#C0C0C0') # 银色 if i==1: # 第一名 rect.set_facecolor('#FFFACD') # 金色 w = rect.get_width() ax.text(w, rect.get_y()+rect.get_height()/2, ' %.2f%%'%w,ha='left', va='center') i -= 1plt.xticks([]) # 关掉横坐标
最后的结果就长这个样子:关键词:星座,各个,分析,采集,信息