首页 > 解决方案 > 我怎样才能刮一个

问题描述

我目前正在为不同的网站编写价格跟踪器,但遇到了问题。我正在尝试h1使用 BeautifulSoup4 抓取标签的内容,但我不知道如何。我尝试使用字典,如 https://stackoverflow.com/a/40716482/14003061中所建议的,但它返回了None. 有人可以帮忙吗?将不胜感激!

这是代码:

from termcolor import colored
import requests
from bs4 import BeautifulSoup
import smtplib

def choice_bwfo():
    print(colored("You have selected Buy Whole Foods Online [BWFO]", "blue"))
    url = input(colored("\n[ 2 ] Paste a product link from BWFO.\n", "magenta"))
    url_verify = requests.get(url, headers=headers)
    soup = BeautifulSoup(url_verify.content, 'html5lib')

    item_block = BeautifulSoup.find('h1', {'itemprop' : 'name'})
    print(item_block)

choice_bwfo()

这是您可以使用的示例 URL:

https://www.buywholefoodsonline.co.uk/organic-spanish-bee-pollen-250g.html

谢谢 :)

标签: python-3.xbeautifulsoup

解决方案


此脚本将打印<h1>标签的内容:

import requests
from bs4 import BeautifulSoup


url = 'https://www.buywholefoodsonline.co.uk/organic-spanish-bee-pollen-250g.html'

# create `soup` variable from the URL:
soup = BeautifulSoup(requests.get(url).content, 'html.parser')

# print text of first `<h1>` tag:
print(soup.h1.get_text())

印刷:

Organic Spanish Bee Pollen 250g

或者你可以这样做:

print(soup.find('h1', {'itemprop' : 'name'}).get_text())

推荐阅读