Python高级应用程序设计
用Python实现一个面向主题的网络爬虫程序,并完成以下内容:
(注:每人一题,主题内容自选,所有设计内容与源代码需提交到博客园平台)
一、主题式网络爬虫设计方案(15分)
1.主题式网络爬虫名称
大众点评网泉州美食数据爬取
2.主题式网络爬虫爬取的内容与数据特征分析
从大众点评网上爬取泉州美食html页面数据,包括'店名', '点评数', '花费', '菜系', '地点', '口味', '环境', '服务'等分析。
3.主题式网络爬虫设计方案概述(包括实现思路与技术难点)
通过numpy函数、requests库、os模块获取网页数据
BeautifulSoup库解析页面内容
padads库将爬取的数据保存为csv形式
pandas数据清洗和处理
matplotilb库可视化数据绘图
二、主题页面的结构特征分析(15分)
1.主题页面的结构特征
1.主题页面的结构特征

2.Htmls页面解析
import requests
from bs4 import BeautifulSoup
url = 'http://www.dianping.com/quanzhou/ch10'
def getHTMLText(url,timeout=30):
try:
r=requests.get(url,timeout=30)
r.raise_for_status()
r.encoding=r.apparent_encoding
return r.text
except:
return '产生异常'
html = getHTMLText(url)
soup=BeautifulSoup(html,'html.parser')
print(soup.prettify())

3.节点(标签)查找方法与遍历方法
(必要时画出节点树结构)
爬虫程序主体要包括以下各部分,要附源代码及较详细注释,并在每部分程序后面提供输出结果的截图。
1.数据爬取与采集
(必要时画出节点树结构)
查找方法:find
遍历方法:for循环嵌套
三、网络爬虫程序设计(60分)爬虫程序主体要包括以下各部分,要附源代码及较详细注释,并在每部分程序后面提供输出结果的截图。
1.数据爬取与采集
#导入库
import requests
from bs4 import BeautifulSoup
import csv
import os
# 链接url
def gethtml(num):
try:
number = num + 1
print('{:<2d}{:<}{:<}'.format(number,'页',':'))#打印正在爬取的页数
url = 'https://www.dianping.com/quanzhou/ch0' + str(num)
r = requests.get(url)
r.raise_for_status()
r.encoding = 'utf-8'#转码
return r.text
except Exception as e:
print(e)
return ''
# 爬取数据
def findhtml(text, ul):
soup = BeautifulSoup(text, 'lxml')
links = soup.find_all('li', class_='')
for link in links:
ui = []
if link.h4 != None:#爬取店铺名
ui.append(link.h4.string)
print('{:^50s}'.format(link.h4.string))#打印店铺名
a1 = link.find('a', class_='review-num')#爬取点评数
if a1:
ui.append(a1.b.string)
else:
ui.append(' ')
a2 = link.find('a', class_='mean-price')#爬取花费
try:
if a2:
ui.append(a2.b.string)
else:
ui.append(' ')
except:
ui.append('')
a3 = link.find('a', {'data-midas-extends': 'module=5_ad_kwcat'})#爬取菜系
if a3:
ui.append(a3.string)
else:
ui.append(' ')
a4 = link.find('a', {'data-midas-extends': 'module=5_ad_kwregion'})#爬取口味,环境,服务
span1 = link.find('span', {'class': 'addr'})
if a4 and span1:
ui.append(a4.string + ' ' + span1.string)
elif a4 == None and span1 != None:
ui.append(span1.string)
elif a4 != None and span1 == None:
ui.append(a4.string)
else:
ui.append(' ')
try:
spans = link.find('span', class_='comment-list')
spanss = spans.contents
ui.append(spanss[1].b.string)
ui.append(spanss[3].b.string)
ui.append(spanss[5].b.string)
except:
ui.append('')
ui.append('')
ui.append('')
ul.append(ui)
# 保存数据,路径D盘
def savehtml(uls):
path = 'D://数据'
if not os.path.exists(path):
os.makedirs(path)
with open(os.path.join(path, '大众点评南京美食.csv'),'w+') as f:
writer = csv.writer(f)
writer.writerow(['店名', '点评数', '菜系', '地点', '口味', '环境', '服务','人流量','人均花费'])
for i in range(len(uls)):
try:
if uls[i]:
writer.writerow(
[uls[i][0], uls[i][1], uls[i][2], uls[i][3], uls[i][4], uls[i][5], uls[i][6], uls[i][7]])#写入csv文件
except:
continue
# main()
def main(i):
ulist = []
it = int(i)
for number in range(it):
html = gethtml(number)
findhtml(html, ulist)
savehtml(ulist)
yeshu = input('输入要查询的总页数(1~50):')
main(yeshu)

2.对数据进行清洗和处理
#导入数据集
import pandas as pd
data=pd.DataFrame(pd.read_excel('D://数据.xls'))
#获取目标信息
def getData(titleList,nameList,numList,html):
#创建BeautifulSoup对象
soup = BeautifulSoup(html,"html.parser")
#获取标题信息
for a in soup.find_all("a",{"class":"title"}):
#将标题信息存在列表中
titleList.append(a.string)
for i in soup.find_all("i",{"class":"acnick"}):
nameList.append(i.string)
for i in soup.find_all("i",{"class":"js-num"}):
List.append(i.string)
数据.drop('点评数',axis=1,inplace=True) #删除无效列点评数
数据.head()#显示前五行
数据.drop('点赞数',axis=1,inplace=True) #删除无效列点赞数
数据.head()#显示前五行
数据.describe()
3.文本分析(可选):jieba分词、wordcloud可视化
4.数据分析与可视化
(例如:数据柱形图、直方图、散点图、盒图、分布图、数据回归分析等)
4.数据分析与可视化
(例如:数据柱形图、直方图、散点图、盒图、分布图、数据回归分析等)
#饼图
import matplotlib.pyplot as plt
Labels = ['日本菜', '西餐', '海鲜', '火锅']
Data = [356,92,45,14,121,194]
#绘制饼图
plt.pie(Data ,labels=Type, autopct='%1.1f%%')
plt.axis(aspect='equal') #将横、纵坐标轴标准化处理,设置显示图像为正圆
plt.rcParams['font.sans-serif']=['SimHei'] #设置字体样式
plt.title('菜系分布')
plt.show()

5.数据持久化
import requests
from bs4 import BeautifulSoup
import os
url = "http://www.dianping.com/quanzhou/ch10"
html = getHTMLText(url) #获取html页面代码
filelist = [] #存储文件名
getFile(html,filelist)
urllist = []
getURL(html,urllist)
def getHTMLText(url): #请求url链接
try:
r = requests.get(url,timeout=30)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except:
return "产生异常"
def getFile(html,filelist): #获取文件名
soup = BeautifulSoup(html,"html.parser")
for p in soup.find_all("p"): #遍历所有属性为p的p标签
for a in p.find_all("a"): #遍历p标签中的a标签
return urllist
def dataStore(file,name): #数据存储
try:
os.mkdir("D:\数据表")
except:
""
try:
with open("D:\\数据表\\{}.xls".format(name),"wb") as fp:#创建文件存储爬取到的数据
fp.write(file.content)
print("下载成功")
except:
"存储失败"
for i in range(0,len(urllist)):
#将目标信息存储在本地
file=requests.get(urllist[i])
dataStore(file,filelist[i])
四、结论(10分)
1.经过对主题数据的分析与可视化,可以得到哪些结论?
1.经过对主题数据的分析与可视化,可以得到哪些结论?
通过以上的一系列完整的数据爬取再进行可视化分析,可以得出泉州作为老城区,其美食的地域分布较为广泛,人流量相对来说符合我国二线城市基本情况,在通过点赞数,评论数,口味服务等的一系列数据分析可很明确看出泉州美食不仅样式多样且独居吸引人眼球,在大众网上也是广受好评。由于部分情况,价格这数据可能有在变化,爬取过程中部分不可见,但是总起还是可以看出价格实惠,可推荐外来人旅游介绍一个特点。
2.对本次程序设计任务完成的情况做一个简单的小结。
网上有很多很多关于这一类东西的教程,我也仅仅是根据嵩老师课程以及上课听讲可以实现,并且停留在一知半解的程度,在代码过程中添加了很多对Python的新的理解,对编程这个大集合的更深层的理解。一开始先要搞清楚什么是爬虫。其实本质上来说爬虫就是一段程序代码。其实任何程序语言都可以做爬虫,只是python语言和其他语言繁简程度不同而已。从定义上来说,爬虫就是模拟用户自动浏览并且保存网络数据的程序,当然,大部分的爬虫都是爬取网页信息,总之,通过实战让我对爬虫新的认识。
浙公网安备 33010602011771号