简体   繁体   English

使用漂亮的汤在 python 中进行网络抓取:AttributeError: 'NoneType' object 没有属性 'text'

[英]web-scraping in python using beautiful soup: AttributeError: 'NoneType' object has no attribute 'text'

I am working on a webscraper using html requests and beautiful soup (I am new to this).我正在使用 html 请求和漂亮的汤(我是新手)开发一个网络爬虫。 For 1 webpage ( https://www.superdrug.com/Make-Up/Face/Primer/Face-Primer/Max-Factor-False-Lash-Effect-Max-Primer/p/788724 ) I am trying to scrape the price of the product.对于 1 个网页( https://www.superdrug.com/Make-Up/Face/Primer/Face-Primer/Max-Factor-False-Lash-Effect-Max-Primer/p/788724 )我试图刮产品的价格。 The HTML is: HTML 是:

<span class="pricing__now" itemprop="price">8.99</span>

I have tried using soup.find and soup.find_all:我尝试过使用soup.find 和soup.find_all:

r = session.get(link)
r.html.render(sleep=3, timeout=30)
soup = BeautifulSoup(r.content, 'lxml')
price = soup.find('span', itemprop="price").text
r = session.get(link)
r.html.render(sleep=3, timeout=30)
soup = BeautifulSoup(r.content, 'lxml')
price = soup.find_all('span', itemprop="price").text

and r.html.find:和 r.html.find:

r = session.get(link)
r.html.render(sleep=6, timeout=30)
price = r.html.find('body > div.pdp-container > div.content-wrapper.pdp > div > div > div.pdp__purchase-options > div.pricing > span:nth-child(2)', first=True).text

None and empty lists are returned, or an AttributeError: 'NoneType' object has no attribute 'text' .返回None和空列表,或者AttributeError: 'NoneType' object has no attribute 'text' I am unsure of why I cannot get this information out.我不确定为什么我无法获取这些信息。 Any help would be appreciated.任何帮助,将不胜感激。

You can get the price from Json data embedded within the page.您可以从页面中嵌入的 Json 数据中获取价格。 For example:例如:

import json
import requests
from bs4 import BeautifulSoup

url = "https://www.superdrug.com/Make-Up/Face/Primer/Face-Primer/Max-Factor-False-Lash-Effect-Max-Primer/p/788724"
headers = {
    "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:87.0) Gecko/20100101 Firefox/87.0"
}

soup = BeautifulSoup(requests.get(url, headers=headers).content, "html.parser")

data = json.loads(soup.select('[type="application/ld+json"]')[1].contents[0])

# uncomment this to print all data:
# print(json.dumps(data, indent=4))

print(data["offers"]["price"])

Prints:印刷:

8.99

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取 AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;text&#39;(网络抓取) - Getting AttributeError: 'NoneType' object has no attribute 'text' (web-scraping) AttributeError: 'NoneType' object 没有属性'find_all' Python Web 刮花 - AttributeError: 'NoneType' object has no attribute 'find_all' Python Web Scraping w/ Beautiful Soup 使用 Selenium BeautifulSoup 进行 Web 抓取的 .text.strip() 上的错误(AttributeError:&#39;NoneType&#39; 对象没有属性 &#39;text) - Error on .text.strip() using Selenium BeautifulSoup for Web-scraping (AttributeError: 'NoneType' object has no attribute 'text) AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;get_text&#39; python web-scraping - AttributeError: 'NoneType' object has no attribute 'get_text' python web-scraping AttributeError: &#39;NoneType&#39; 对象在 beautifulsoop web-scraping 中没有属性 &#39;get_text&#39; - AttributeError: 'NoneType' object has no attribute 'get_text' in beautifulsoop web-scraping 美丽的汤:AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;text&#39; - Beautiful soup: AttributeError: 'NoneType' object has no attribute 'text' python - 'AttributeError: 'NoneType' object 在 web 抓取时没有属性 'text' - python - 'AttributeError: 'NoneType' object has no attribute 'text' when web scraping 通过美丽的汤 4 python 抓取时,错误 nonetype 对象没有属性文本 - Error nonetype object has no attribute text while scraping via beautiful soup 4 python 使用BeautifulSoup的Python Web爬网AttributeError:&#39;NoneType&#39;对象没有属性&#39;text&#39; - Python Web Scraping using BeautifulSoup AttributeError: 'NoneType' object has no attribute 'text' 在一个混乱的网站上使用美丽汤进行Python Web抓取 - Python Web-Scraping using Beautiful Soup on a messy Site
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM