简体   繁体   English

AttributeError: 'NoneType' object 没有带有输入 id 的属性 'get_text'

[英]AttributeError: 'NoneType' object has no attribute 'get_text' with input id

url = 'https://www.amazon.ca/Powerextra-Replacement-Battery-Compatible-NP-FZ100/dp/B07PQQQ82K/ref=bmx_4?pd_rd_w=TNcoX&pf_rd_p=d9347c84-a27a-4c22-b959-7bb71382273b&pf_rd_r=E7RKN5D6HSM4TFZQJCGT&pd_rd_r=e8bff283-bc96-4b77-b31a-f413b4d62e40&pd_rd_wg=wje3Z&pd_rd_i=B07PQQQ82K&psc=1'
headers = 'headers = { "user-Agent": 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'}'
page = requests.get(url, headers= headers)
soup = BeautifulSoup(page.content, "lxml")

title = soup.find('input', {'id': 'mbb-offeringID-1'}).get_text()
print(title)

I also tried:我也试过:

title = soup.find('input', {'id': 'mbb-offeringID-1'}).get('value')
print(title)

Then I tried if it works in general with a "span id" and it gave the same error然后我尝试了它是否通常与“span id”一起工作并且它给出了相同的错误

title = soup.find(id = "productTitle").get_text()

Try to add "Accept-Language": "en-US,en;q=0.5" HTTP header for correct response from the server:尝试添加"Accept-Language": "en-US,en;q=0.5" HTTP header 以获得服务器的正确响应:

import requests
from bs4 import BeautifulSoup


url = "https://www.amazon.ca/Powerextra-Replacement-Battery-Compatible-NP-FZ100/dp/B07PQQQ82K/ref=bmx_4?pd_rd_w=TNcoX&pf_rd_p=d9347c84-a27a-4c22-b959-7bb71382273b&pf_rd_r=E7RKN5D6HSM4TFZQJCGT&pd_rd_r=e8bff283-bc96-4b77-b31a-f413b4d62e40&pd_rd_wg=wje3Z&pd_rd_i=B07PQQQ82K&psc=1"
headers = headers = {
    "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:88.0) Gecko/20100101 Firefox/88.0",
    "Accept-Language": "en-US,en;q=0.5",
}
page = requests.get(url, headers=headers)
soup = BeautifulSoup(page.content, "lxml")

title = (
    soup.find("input", {"id": "mbb-offeringID-1"})
    .find_next("span")
    .get_text(strip=True, separator=" ")
)
print(title)

Prints:印刷:

Asurion 3-Year Protection Plan for $14.99

暂无
暂无

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

相关问题 Python-AttributeError:“ NoneType”对象没有属性“ get_text” - Python - AttributeError: 'NoneType' object has no attribute 'get_text' AttributeError: 'NoneType' 对象没有属性 'get_text' - AttributeError: 'NoneType' object has no attribute 'get_text' “AttributeError: 'NoneType' object 没有属性 'get_text'” - "AttributeError: 'NoneType' object has no attribute 'get_text'" AttributeError: 'NoneType' 对象没有属性 'get_text' , id='productTitle is there in URL - AttributeError: 'NoneType' object has no attribute 'get_text' ,id='productTitle is there in URL 在亚马逊网页抓取时在 BS4 中收到错误:AttributeError: 'NoneType' 对象没有属性 'get_text' - Receiving an error in BS4 while amazon web scraping : AttributeError: 'NoneType' object has no attribute 'get_text' AttributeError: 'NoneType' 对象在 beautifulsoop web-scraping 中没有属性 'get_text' - AttributeError: 'NoneType' object has no attribute 'get_text' in beautifulsoop web-scraping AttributeError: 'NoneType' 对象没有属性 'get_text' python web-scraping - AttributeError: 'NoneType' object has no attribute 'get_text' python web-scraping AttributeError: 'NoneType' 对象没有属性 'get_text' python 3x - AttributeError: 'NoneType' object has no attribute 'get_text' python 3x AttributeError:'NoneType'对象没有属性'get_text',我该如何解决? - AttributeError: 'NoneType' object has no attribute 'get_text' how can I solve it? AttributeError: 'NoneType' 对象没有属性 'get_text'。 BeautifulSoup 对象失败 - AttributeError: 'NoneType' object has no attribute 'get_text'. BeautifulSoup objects failing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM