我收到AttributeError:“ NoneType”对象没有属性“ find_all”

时间:2019-11-27 07:42:08

标签: python-3.x web-scraping

大家好,我正在尝试为老板完成一个项目,并且遇到一个问题,每次我运行代码时,都会出现错误AttributeError:'NoneType'对象没有属性'find_all',现在我根本就不是python编码器,真的,我什么时候都能感觉到,我检查了页面的后端,所有div标签看起来都不错,并且在网络上进行了很好的搜索,而我的菜鸟眼睛看起来还不错但我似乎无法使其正常工作。我试图进入页面并将每个排期详细信息放在各自的元素中,以便将其保存到MySQL数据库,这是代码

import requests
from requests_html import HTMLSession
from bs4 import BeautifulSoup
import mysql.connector

page = requests.get("https://www.adelaideairport.com.au/flight-information/flight-search/")
soup = BeautifulSoup(page.content, 'html.parser')
flights = soup.find(id="SearchResultFlightListTable")
flight_items = flights.find_all(class_="row")
flight = flight_items[0]
print(flight.prettify())

2 个答案:

答案 0 :(得分:0)

现在一切正常

    from requests_html import HTMLSession
import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="****",
  database="flightdata"
)

mycursor = mydb.cursor()

# create an HTML Session object
session = HTMLSession()

# Use the object above to connect to needed webpage
resp = session.get("https://www.adelaideairport.com.au/flight-information/flight-search/?flt_no=&carrier=All&city=&dte=Current&leg=Departures")

# Run JavaScript code on webpage
resp.html.render()


airline_spans = resp.html.find('.SearchResultFlightListRow')
airline_list = [span.text.split('\n') for span in airline_spans]

for flight in airline_list:
    if len(flight) == 7:
        flightno, From, to, scheduled, estimated, gate, status = flight
        print (f'Flight no {flightno} from  {From} to {to} is scheduled to depart at {scheduled} from gate {gate} and flight status is {status}')

    elif len(flight) == 6:
        flightno, From, to, scheduled, estimated, gate = flight
        status = 'IDEL'
        print (f'Flight no {flightno} from  {From} to {to} is scheduled to depart at {scheduled} from gate {gate} and flight status is {status}')

    elif len(flight) == 5:
        flightno, From, to, scheduled, estimated = flight
        gate = 'IDEL'
        statua = 'IDEL'
        print (f'Flight no {flightno} from  {From} to {to} is scheduled to depart at {scheduled} from gate and flight status is {status} {gate}')


   sql = "INSERT INTO flightinfo (From, id, airline, to, flightno, scheduled, estimatedTime, scheduledDate, latestTime, status) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"

    val = (origin, flight_id, airline, " ".join(destination), ", ".join(flightNumbers), scheduledTime, estimatedTime,
            scheduledDate, latestTime, status)
    data.append(val)


# doing a batch insert
mycursor.executemany(sql, data)

mydb.commit()

print(mycursor.rowcount, "was inserted.")
#replace with code to add flight details to database


#print(airline_list)

答案 1 :(得分:0)

实际上,您正在搜索id = SearchResultFlightListTable,但这是一个类,所以这就是为什么您遇到NoneType错误的原因
我已经修改了一些代码,您可以在下面参考

import requests
from bs4 import BeautifulSoup

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

page = requests.get("https://www.adelaideairport.com.au/flight-information/flight-search/")
soup = BeautifulSoup(page.content, 'html.parser')
flights = soup.find(class_="SearchResultFlightListTable")
flight_items = flights.find_all(class_="row")
flight = flight_items[0]
print(flight.prettify())