脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python爬取附近餐馆信息代码示例

Python爬取附近餐馆信息代码示例

2020-12-23 00:45zhglance Python

这篇文章主要介绍了Python爬取附近餐馆信息代码示例,具有一定借鉴价值,需要的朋友可以参考下。

本代码主要实现抓取大众点评网中关村附近的餐馆有哪些,具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import urllib.request
import re
 
def fetchFood(url):
  # 模拟使用浏览器浏览大众点评的方式浏览大众点评
  headers = {'User-Agent',
        'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36'}
  opener = urllib.request.build_opener()
  opener.addheaders = [headers]
  data = opener.open(url).read()
  data = data.decode('utf')
   print('================================抓取的页面数据=======================================')
  print(data)   # 打印抓取的页面
   print('================================获取的餐馆信息====================================')
  foodNameSet = set(re.compile(r'<img id="codetool">

输出结果:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"D:\ProgramFiles\python\python.exe"D:/zhangzh/program/MyGitHub/python3-webapp-demo/www/dzdp.py
================================抓取的页面数据=======================================
<!DOCTYPEhtml>
<html>
<head>
<metacharset="UTF-8"/>
<metahttp-equiv="X-UA-Compatible"content="IE=edge"/>
<script>(function(n){vare;e="//catdot.dianping.com/broker-service/api/js",n.onerror=function(n,o,r){vari=encodeURIComponent,t=+newDate();(newImage).src=e+"?error="+i(n)+"&file="+i(o)+"&line="+i(r)+"×tamp="+t}})(window);</script>
<title>北京中关村美食-大众点评网</title>
......此处省略
<liclass="">
<divclass="pic">
<atarget="_blank"href="/shop/33562041" rel="external nofollow" rel="external nofollow" rel="external nofollow" title="">
<imgtitle="渝是乎(中关村店)"alt="渝是乎(中关村店)"data-src="http://p0.meituan.net/ugcpic/023ff4be1a239be5b7f13ac328bc6c5d%40249w_249h_0e_1l%7Cwatermark%3D1%26%26r%3D1%26p%3D9%26x%3D2%26y%3D2%26relative%3D1%26o%3D20"/>
</a>
</div>
<divclass="txt">
<divclass="tit">
<aonclick="document.hippo.ext({cl_i:10,query_id:'4ab097b5-d3b6-441b-a684-8b58c0704567'}).mv('cl_to_s',33562041);
"data-hippo-type="shop"title="渝是乎(中关村店)"target="_blank"href="/shop/33562041" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
<h4>渝是乎(中关村店)</h4>
</a>
<divclass="promo-icon">
<atarget="_blank"href="/shop/33562041#waimai" rel="external nofollow"
class="iout"title="本店支持在线下单,足不出户,外送到家!"></a>
</div>
<atarget="_blank"href="/search/branch/2/0_33562041/g0" rel="external nofollow"
module="list-branch"
class="shop-branch">分店</a>
</div>
<divclass="comment">
<spanclass="sml-rank-starssml-str50"title="五星商户"></span>
<ahref="/shop/33562041#comment" rel="external nofollow" class="review-num"target="_blank"module="list-readreview"
>
<b>1536</b>
条点评</a>
<emclass="sep">|</em>
<ahref="/shop/33562041" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="mean-price"target="_blank">
人均
<b>¥42</b>
</span>
</a>
</div>
<divclass="tag-addr">
<ahref="/search/category/2/10/g102" rel="external nofollow" ><spanclass="tag">川菜</span></a>
<emclass="sep">|</em>
<ahref="/search/category/2/0/r1488" rel="external nofollow" ><spanclass="tag">中关村</span></a>
<spanclass="addr">榆树林1号</span>
</div>
<spanclass="comment-list">
<span>口味<b>9.1</b></span>
<span>环境<b>8.7</b></span>
<span>服务<b>8.3</b></span>
</span>
</div>
<divclass="svr-info">
......此处省略
</script>
</body></html>
================================获取的餐馆信息====================================
1个餐馆:重八牛府(之初入江湖店)
2个餐馆:纽约客美式餐厅(新中关店)
3个餐馆:Chatuchak加都加曼谷潮流甜品
4个餐馆:新净雅烹小鲜
5个餐馆:那家小馆(中关村店)
6个餐馆:谷得一
7个餐馆:唐廊.朴禅(当代商城店)
8个餐馆:小福楼餐厅
9个餐馆:食宝街
10个餐馆:渝是乎(中关村店)
11个餐馆:品咖啡
12个餐馆:小吊梨汤(融科店)
13个餐馆:鱼八斗老麻水煮鱼(酸菜鱼)
14个餐馆:鳗鳗的爱(新中关购物中心店)
15个餐馆:速度牛排
Processfinishedwithexitcode0

总结

以上就是本文关于Python爬取附近餐馆信息代码示例的全部内容,希望对大家有所帮助。如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

原文链接:http://blog.csdn.net/zhglance/article/details/68486787

延伸 · 阅读

精彩推荐