当前位置:首页 >> 脚本专栏

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

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

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 title="(.*"', re.DOTALL).findall(data)) 
  number = 0 
  for i in foodNameSet: 
    number = number + 1 
    print("第%d个餐馆: %s" % (number, i)) 
foodUrl = "http://www.dianping.com/search/category/2/10/r1488" 
fetchFood(foodUrl) 

输出结果:

 
"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+""+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="/UploadFiles/2021-04-08/023ff4be1a239be5b7f13ac328bc6c5d%40249w_249h_0e_1l%7Cwatermark%3D1%26%26r%3D1%26p%3D9%26x%3D2%26y%3D2%26relative%3D1%26o%3D20">

总结

以上就是本文关于Python爬取附近餐馆信息代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:

python登录并爬取淘宝信息代码示例

Python爬取当当、京东、亚马逊图书信息代码实例

matplotlib在python上绘制3D散点图实例详解

如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!