【问题标题】:BeautifulSoup scrape itemprop="name" in PythonBeautifulSoup 在 Python 中抓取 itemprop="name"
【发布时间】:2017-07-28 05:27:47
【问题描述】:

我有一些 python 3.5 代码,我想用它来抓取网页的一部分,但不是打印“又厚又耐嚼的花生酱巧克力条”,而是打印“无”。你知道为什么吗?谢谢。

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

【问题讨论】:

    标签: python python-3.x web-scraping beautifulsoup


    【解决方案1】:

    instructions = recipeSoup.find("div", itemprop="name") 更改为instructions = recipeSoup.find("span", itemprop="name") 以获取配方标题。

    有关说明,您必须使用 itemprop=ingredients 搜索 li 标签。

    【讨论】:

      猜你喜欢
      • 2013-09-28
      • 2016-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-08
      • 2018-04-25
      • 2014-06-20
      相关资源
      最近更新 更多