python抓取网页数据处理后可视化

抓取文章的链接,访问量保存到本地

 1 #coding=utf-8
 2 import requests as req
 3 import re
 4 import urllib
 5 from bs4 import BeautifulSoup
 6 import sys
 7 import codecs
 8 import time
 9 
10     
11 r=req.get('https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000',
12          headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'})
13 content=r.text
14 #print(content)
15 soup=BeautifulSoup(content,'html.parser')
16 
17 #下面2行内容解决UnicodeEncodeError: 'ascii' codec can't encode characters in position 63-64问题,但是加了后print就打印不出来了,需要查原因
18 reload(sys)
19 sys.setdefaultencoding('utf-8')
20 
21 i=0
22 for tag in soup.find_all(re.compile(r'^a{1}'),{'class':'x-wiki-index-item'}):
23     i=i+1
24     if i%3==0:
25         time.sleep(30)
26     name=tag.get_text()
27     href='https://www.liaoxuefeng.com'+tag['href']
28     req2=req.get(href,headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'})
29     time.sleep(3)
30     soup2=BeautifulSoup(req2.text,'html.parser')
31     count=soup2.find_all('div',{'class':'x-wiki-info'})
32     try:
33         co=count[0].find('span').get_text()
34         co=co[7:]
35     except IndexError as e:
36         co='0'
37     with open('E:/sg_articles.xlsx', 'a+') as f:
38         f.write(codecs.BOM_UTF8)#解决写入csv后乱码问题
39         f.write(name+','+href+','+co+'\n')
40 '''
41 睡眠是因为网页访问过多就会报503 Service Unavailable for Bot网站超过了iis限制造成的由于2003的操作系统在提示IIS过多时并非像2000系统提示“链接人数过多”
42 http://www.51testing.com/html/53/15150753-4455790.html --数据可视化
43 http://www.cnblogs.com/xxoome/p/5880693.html --python引入模块时import与from ... import的区别
44 https://www.cnblogs.com/amou/p/9184614.html --讲解几种爬取网页的匹配方式
45 https://www.cnblogs.com/yinheyi/p/6043571.html --python基本语法
46 '''

上面代码的思路:先获取主网页,再遍历主网页上的文章链接并请求这些链接以进入子网页,从而获得子网页中span标签保存的访问量。

下面打开本地文件,pandas进行数据分析,然后pyecharts实现图形化

 1 #coding=utf-8
 2 from pyecharts import Bar
 3 import pandas as pd
 4 
 5 p=pd.read_excel('E:\sg_articles.xls',names=["title","href","count"])
 6 a=p.sort_values(by='count',ascending=False)[0:3]
 7 title=a['title']
 8 count=a['count']
 9 bar=Bar("点击量TOP3", title_pos='center', title_top='18', width=800, height=400)
10 bar.add("", title, count, is_convert=True, xaxis_min=10, yaxis_rotate=30, yaxis_label_textsize=10, is_yaxis_boundarygap=True, yaxis_interval=0,
11         is_label_show=True, is_legend_show=False, label_pos='right',is_yaxis_inverse=True, is_splitline_show=False)
12 bar.render("E:\点击量TOP3.html")

最后的结果

同时还有很多疑问,需要懂的朋友帮忙解答:

1.第一段代码中保存的xlsx格式,实际保存后打开是提示有损坏的,使用xml打开则就没有问题,另存为xls格式后打开也没有问题

2.访问量过多会报错,我就利用了sleep,但实际上访问量间断性的读取到了值,有的为什么读不到值了?

3.利用sort_values对count列进行排序取前3,这个会自动排除掉excel格式的第一行,不知道是为什么

感觉后面我还需要强化1)pandas数据处理的方法,比如分组排序等

扫描二维码关注公众号,回复: 4393517 查看本文章

2)正则表达式的提取

3)pyecharts的图形绘制

4)在遇到网页有反爬情况下的虚拟ip设置等

记录我的python学习之路,大家一起努力哟~~

猜你喜欢

转载自www.cnblogs.com/mwg666/p/10072564.html
今日推荐