2017-03-07 85 views
0

我有一些python 3.5代碼,我想刮一部分的網頁,但不打印「厚厚的和耐嚼的花生醬巧克力芯片」它打印「無」。你知道爲什麼嗎?謝謝。在Python中BeautifulSoup刮itemprop =「名稱」

import requests, bs4 
import tkinter as tk 
from tkinter import * 
import pymysql 
import pymysql.cursors 

res = requests.get("http://www.foodnetwork.co.uk/article/traybake-recipes/thick-and-chewy-peanut-butter-chocolate-chip-bars/list-page-2.html") 
res.raise_for_status() 
recipeSoup = bs4.BeautifulSoup(res.text, "html.parser") 
type(recipeSoup) 
instructions = recipeSoup.find("div", itemprop="name") 
try: 
    method = str.replace(instructions.get_text(strip=True),". ",".") 
    method = str.replace(method, ". ", ".") 
    method = (str.replace(method, ".",".\n")) 
except AttributeError: 
    print(instructions) 

Link to scraped page

回答

1

變化instructions = recipeSoup.find("div", itemprop="name")instructions = recipeSoup.find("span", itemprop="name")拿到配方標題。

有關說明,您必須搜索li標籤itemprop=ingredients