python-3.x - 我怎样才能刮一个
问题描述
我目前正在为不同的网站编写价格跟踪器,但遇到了问题。我正在尝试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
谢谢 :)
解决方案
此脚本将打印<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())
推荐阅读
- mysql - collect_set() 仅当用户超过过去 90 天时,按天从过去 90 天内区分用户
- javascript - Discord制作需要确认的频道机器人
- c - 如何在c中创建一个不丢失内存的链表?
- python - pygame中移动精灵之间的碰撞
- python-3.x - windows 10 pyautogui 多个桌面
- mongodb - 如何使用 Strapi 在 GraphQL 中添加自定义查询?
- python - 使用 BeautifulSoup 从主跨度类下的多个跨度中检索数据
- kubernetes - pod 和服务外部 IP 相同的 IP
- python - 让一个函数调用另一个创建元组的函数
- classification - MNIST 数据集提升