簡體   English   中英

屬性錯誤“NoneType”對象沒有屬性“find_all”

[英]attribute error 'NoneType' object has no attribute 'find_all'

導入請求

從 bs4 導入 BeautifulSoup

r = requests.get('https://www.preparedfoods.com/articles/106989-irish-pork-crisis')

湯 = BeautifulSoup(r.content, 'html.parser')

打印(湯。標題)

s = soup.find('div', class_='entry-content')

行 = s.find_all('p')

對於行中的行: print(line.text)

愛爾蘭豬肉危機 | 預制食品

AttributeError Traceback (last last call last) in () 17 s = soup.find('div', class_='entry-content') 18 ---> 19 lines = s.find_all('p') 20 21 for line在行中:

AttributeError:“NoneType”對象沒有屬性“find_all”

if塊內添加行

s = soup.find('div', class_='entry-content')
if s is not None:
    lines = s.find_all('p')
    for line in lines: print(line.text)

可能您嘗試獲取帶有標題的文章。 所以你重新使用了錯誤的課程。 將其替換為{'class': ['body' 'gsd-paywall', 'article-body']}你會得到結果

from bs4 import BeautifulSoup
import requests


r = requests.get('https://www.preparedfoods.com/articles/106989-irish-pork-crisis')
soup = BeautifulSoup(r.text.replace('</p>', '\n'), 'html.parser')
print(soup.title.get_text())
print(soup.find('div', {'class': ['body' 'gsd-paywall', 'article-body']}).get_text())

輸出:

Irish Pork Crisis | Prepared Foods

December 8/The Irish Times -- An estimated 100,000 pigs will have to be destroyed because of a pigmeat crisis which has led to the recall of all Irish pork products in Ireland's largest food scare since BSE. 
The public have been told to dump or return all pork products which they purchased since September 1st last because of the risk of dioxin contamination.
It is estimated that 125 million euros worth of food products in home and in export markets -- up to 25 countries -- will have to be destroyed. 
The recall followed the discovery of potentially dangerous dioxins, known as PCBs, in pigmeat. They were initially traced in an unnamed meat plant in the republic. The dioxins were contained in feed supplied from a Co Carlow food recycling plant, it emerged.
          
As the government moved to ease the fears of consumers, investigations were continuing at 10 pig farms and 38 beef farms in the republic. The contamination is likely to have a severe impact on the 7 billion euro Irish food industry.
Contaminated feed from the Co Carlow facility, Millstream Recycling in Clohamon Mills, had also been supplied to nine farms in Northern Ireland which now have been restricted.
The investigation has found contaminated pork with dioxin levels of 80-200 times above the safety limits. It is being led by the Departments of Agriculture and Health, and the Food Safety Authority of Ireland (FSAI). The Garda Síochána are also involved.
          
The dramatic food recall was announced as the investigation into the source of the contamination, understood to be oil, was stepped up after tests at a U.K. laboratory in York confirmed the presence of dioxins in the pigmeat.
The crisis began, however, last month when a routine sample was taken from the meat plant. 
Other examination of Irish products in the Netherlands, France and Belgium prompted the action by the government in an attempt to protect consumer confidence at home and abroad.
The European Commission has called a meeting of food safety experts from Ireland and other affected EU states to coordinate a Europe-wide response to the contamination of Irish pork products.
Millstream Recycling has confirmed it has been working with Department of Agriculture officials to identify the source of PCBs found in pig meal used in a number of farms in Ireland. Accepting the need for a recall, Millstream Recycling said it would be carrying out "a full investigation to establish how the company's strict health and safety procedures and the high quality standards could possibly have been breached."
The FSAI repeated its advice to consumers not to eat any pork products, but it said people should not be alarmed or concerned in relation to the potential risks from short-term exposure to dioxins found in pork products.
Dr. Tony Holohan, chief medical officer at the Department of Health, said a number of health studies conducted in Belgium since the dioxin scare in 1999 had not found any negative effects on the population. "From the experience in Belgium, we don't anticipate any health effects and on that basis we are reassuring people."
Professor James Heffron, a specialist on the biochemistry of detoxification at UCC's biochemical toxicology lab, told The Irish Times, however, the government in his view needed to do more to reassure the public. Heffron said information on the amount of dioxin found in affected meat should be released in addition to further details on the duration of exposure. "When we have this information we can relate it to World Health Organization guidelines on acceptable levels of dioxin," he added.
From the December 22, 2008, Prepared Foods e-Flash

您可能正在訪問 GDPR 政策頁面: https ://www.preparedfoods.com/gdpr-policy?url=https%3A%2F%2Fwww.preparedfoods.com%2Farticles%2F106989-irish-pork-crisis

在這種情況下,您需要 selenium 來單擊 Accept/Decline,然后導航到所需的頁面並獲取所需的信息。 這將完成這項工作:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup


chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--headless")


webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary
browser = webdriver.Chrome(service=webdriver_service, options=chrome_options)

url = 'https://www.preparedfoods.com/articles/106989-irish-pork-crisis'

browser.get(url)
WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'input[value="Accept"]')))
accept_button = browser.find_element(By.CSS_SELECTOR, 'input[value="Accept"]')
accept_button.click()
WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.CLASS_NAME, 'article-body')))
result = browser.find_element(By.CLASS_NAME, "article-body").text
print(result)

結果:

December 8/The Irish Times -- An estimated 100,000 pigs will have to be destroyed because of a pigmeat crisis which has led to the recall of all Irish pork products in Ireland's largest food scare since BSE.
The public have been told to dump or return all pork products which they purchased since [...]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM