import requests
import bs4
ua = 'Mozilla/5.0.html (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.html.2171.71 Safari/537.36'
def get_recipe_from_xiachufang(url):
headers = {'User-Agent': ua}
response = requests.get(url, headers=headers)
response.raise_for_status()
soup = bs4.BeautifulSoup(response.text, 'html.parser')
name = soup.find('h1', class_='page-title').text.strip()
name = name.split('的')[-1]
ingredients = []
for ingredient in soup.find('div', class_='ings').find('table').find_all('tr'):
ingredients.append(' / '.join([td.text.strip() for td in ingredient.find_all('td')]))
steps = []
for step in soup.find('div', class_='steps').find('ol').find_all('li'):
step_text = step.find('p').text.strip()
step_img = step.find('img')
if step_img:
steps.append([step_text, step_img['src']])
else:
steps.append([step_text])
return {'name': name, 'ingredients': ingredients, 'steps': steps}