python-python爬取豆果网(菜谱信息)
#-*- coding = utf-8 -*- #获取豆果网图片 import io from bs4 import BeautifulSoup import requests #爬取菜谱的地址 url = "https://www.douguo.com/cookbook/2029254.html" #防止网址的反爬虫 header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'} #获取网页信息 html = requests.get(url,headers = header) #解析网页信息 text = BeautifulSoup(html.content,"lxml") #获取主要菜谱图片的地址(在此仅仅获取了一张图片) img_title = text.select("#banner img") imgg = img_title[0].get("src") #根据url将图片保存到当地 def get_img_data(ul): htm = requests.get(ul,headers = header) filename = "1.jpg" #这个地方的filename可以根据需求进行修改 f = open(filename,"wb") #将图片写到文件中 f.write(htm.content) f.close() get_img_data(imgg) #分别获取菜谱信息 menu_title_0 = text.select('.title.text-lips')[0].text menu_intro = text.select('.intro')[0].text menu_title_1 = text.select('.mini-title')[0].text menu_content_scname = text.find_all('span',class_='scname') menu_content_scnum = text.find_all('span',class_='scnum') menu_title_2 = text.select('.mini-title')[1].text menu_step = text.select('.stepinfo') print(menu_title_0) print(menu_intro) print(menu_title_1) count = 0 for i in menu_content_scname: print(i.text," ",menu_content_scnum[count].text) count = count + 1 print(menu_title_2) for menu_step_i in menu_step: print(menu_step_i.text)