简体   繁体   English

属性错误“NoneType”对象没有属性“find_all”

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

import requests导入请求

from bs4 import BeautifulSoup从 bs4 导入 BeautifulSoup

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

soup = BeautifulSoup(r.content, 'html.parser')汤 = BeautifulSoup(r.content, 'html.parser')

print(soup.title)打印(汤。标题)

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

lines = s.find_all('p')行 = s.find_all('p')

for line in lines: print(line.text)对于行中的行: print(line.text)

Irish Pork Crisis | 爱尔兰猪肉危机 | Prepared Foods 预制食品

AttributeError Traceback (most recent call last) in () 17 s = soup.find('div', class_='entry-content') 18 ---> 19 lines = s.find_all('p') 20 21 for line in lines: 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' object has no attribute 'find_all' AttributeError:“NoneType”对象没有属性“find_all”

Add the lines inside if blockif块内添加行

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

Probably you try to get article with title.可能您尝试获取带有标题的文章。 So u re use wrong class.所以你重新使用了错误的课程。 Replace it to {'class': ['body' 'gsd-paywall', 'article-body']} and you ll get result将其替换为{'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())

OUTPUT:输出:

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

You might be hitting GDPR Policy page: https://www.preparedfoods.com/gdpr-policy?url=https%3A%2F%2Fwww.preparedfoods.com%2Farticles%2F106989-irish-pork-crisis您可能正在访问 GDPR 政策页面: https ://www.preparedfoods.com/gdpr-policy?url=https%3A%2F%2Fwww.preparedfoods.com%2Farticles%2F106989-irish-pork-crisis

In this case, you need selenium to click on Accept/Decline, then navigate to desired page and get the info you need.在这种情况下,您需要 selenium 来单击 Accept/Decline,然后导航到所需的页面并获取所需的信息。 This will do the job:这将完成这项工作:

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)

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