Python crawls a code sample for nearby restaurants

  • 2020-06-15 09:38:36
  • OfStack

This code is mainly to grab dianping.com zhongguancun near the restaurants, the details are as follows:


import urllib.request 
import re 

def fetchFood(url): 
  #  Simulate using a browser to view Dianping  
  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('================================ Crawled page data =======================================') 
  print(data)   #  Print the captured page  
   print('================================ Obtained restaurant information ====================================') 
  foodNameSet = set(re.compile(r'<img title="(.*?)"', re.DOTALL).findall(data)) 
  number = 0 
  for i in foodNameSet: 
    number = number + 1 
    print(" The first %d A restaurant:  %s" % (number, i)) 
foodUrl = "http://www.dianping.com/search/category/2/10/r1488" 
fetchFood(foodUrl) 

Output results:


 
"D:\ProgramFiles\python\python.exe"D:/zhangzh/program/MyGitHub/python3-webapp-demo/www/dzdp.py 
================================ Crawled page data ======================================= 
<!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)+" x tamp="+t}})(window);</script> 
<title> Beijing Zhongguancun Cuisine - Dianping.com </title> 
...... Is omitted  
<liclass=""> 
<divclass="pic"> 
<atarget="_blank"href="/shop/33562041" rel="external nofollow" rel="external nofollow" rel="external nofollow" title=""> 
<imgtitle=" Chongqing is on ( Zhongguancun shop )"alt=" Chongqing is on ( Zhongguancun shop )"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=" Chongqing is on ( Zhongguancun shop )"target="_blank"href="/shop/33562041" rel="external nofollow" rel="external nofollow" rel="external nofollow" > 
<h4> Chongqing is on ( Zhongguancun shop )</h4> 
</a> 
<divclass="promo-icon"> 
<atarget="_blank"href="/shop/33562041#waimai" rel="external nofollow"  
class="iout"title=" This shop supports online ordering, do not leave the house, out to the home! "></a> 
</div> 
<atarget="_blank"href="/search/branch/2/0_33562041/g0" rel="external nofollow"  
module="list-branch" 
class="shop-branch"> branch </a> 
</div> 
<divclass="comment"> 
<spanclass="sml-rank-starssml-str50"title="5 Star merchants "></span> 
<ahref="/shop/33562041#comment" rel="external nofollow" class="review-num"target="_blank"module="list-readreview" 
> 
<b>1536</b> 
 Article reviews </a> 
<emclass="sep">|</em> 
<ahref="/shop/33562041" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="mean-price"target="_blank"> 
 Per capita  
<b> RMB 42</b> 
</span> 
</a> 
</div> 
<divclass="tag-addr"> 
<ahref="/search/category/2/10/g102" rel="external nofollow" ><spanclass="tag"> Sichuan Cuisine </span></a> 
<emclass="sep">|</em> 
<ahref="/search/category/2/0/r1488" rel="external nofollow" ><spanclass="tag"> zhongguancun </span></a> 
<spanclass="addr"> Elm wood 1 No. </span> 
</div> 
<spanclass="comment-list"> 
<span> taste <b>9.1</b></span> 
<span> The environment <b>8.7</b></span> 
<span> service <b>8.3</b></span> 
</span> 
</div> 
<divclass="svr-info"> 
...... Is omitted  
</script> 
</body></html> 
================================ Obtained restaurant information ==================================== 
 The first 1 Restaurant: Heavy 8 Cattle house ( First into the river's lake shop ) 
 The first 2 Restaurant: New Yorker American restaurant ( The new store overspending: ) 
 The first 3 A restaurant: Chatuchak Add Bangkok's trendy desserts  
 The first 4 Restaurant: New clean and elegant small fresh  
 The first 5 Restaurant: The small restaurant ( Zhongguancun shop ) 
 The first 6 Restaurant: Goode 1 
 The first 7 Restaurant: Tang Long . PiaoChan ( Contemporary Mall Store ) 
 The first 8 Restaurant: Xiao Fu Lou Restaurant  
 The first 9 Restaurant: Food Treasure Street  
 The first 10 Restaurants: Yu is Hu ( Zhongguancun shop ) 
 The first 11 A restaurant: Coffee  
 The first 12 Restaurant: Small pear soup ( Financial department store ) 
 The first 13 Restaurants: Fish 8 Water boiled fish with Lao Ma (pickled cabbage fish)  
 The first 14 Restaurants: Eel love ( Xinzhongguan Shopping Center store ) 
 The first 15 Restaurants: Speed steak  
Processfinishedwithexitcode0

conclusion

That's the end of this article's code example of Python crawling for nearby restaurants. Those who are interested can continue to see this site:

python login and crawls taobao information code example

Python crawling Dangdang, Jingdong, Amazon book information code examples

matplotlib draws a detailed example of a 3D scatter plot on python

If there is any deficiency, please let me know. Thank you for your support!


Related articles: