【问题标题】:Python code can't find HTML elementPython 代码找不到 HTML 元素
【发布时间】:2017-05-23 08:03:44
【问题描述】:

在抓取此页面 (http://bobaedream.co.kr/cyber/CyberCar_view.php?no=652455&gubun=I) 时,我的代码返回了我无法理解的错误消息。

在 div 标签 (div class='rightarea') 下,有许多标签。但是当我尝试读取和收集数据时,它不断返回错误消息,(content_table1 = table.find_all('div', class_='information') 'ResultSet' object has no attribute 'find_all')。奇怪的是我的代码没有返回任何错误消息来收集不同列表页面中的这部分数据。

下面是我的代码:

from bs4 import BeautifulSoup
import urllib.request
from urllib.parse import urlparse
from urllib.parse import quote
from selenium import webdriver
import re
import csv

URL = 'http://bobaedream.co.kr/cyber/CyberCar_view.php?no=652455&gubun=I'
res = urllib.request.urlopen(URL)
html = res.read()
soup = BeautifulSoup(html, 'html.parser')

# Basic Information
table = soup.find_all('div', class_='rightarea')
print(table)

# Number, Year, Mileage, Gas Type, Color, Accident
content_table1 = table.find_all('div', class_='information')

请帮忙。

【问题讨论】:

    标签: python html web-crawler


    【解决方案1】:
    table = soup.find_all('div', class_='rightarea') # will return a list like [div_tag, div_tag, ...]
    

    table.find_all('div', class_='information') 等同于:

    [div_tag, div_tag, ...].find_all('div', class_='information')
    

    只有tag对象可以使用find_all(),你应该遍历table list,得到div tag,而不是使用find_all()

    for t in table:
        t.find_all('div', class_='information')
    

    【讨论】:

      【解决方案2】:
      # Basic Information
      table = soup.find_all('div', class_='rightarea')
      print(table)
      

      soup.findall 返回一个 bs4.element.ResultSet。有两个项目。第一个是“'div', class_='information'”。

      (Pdb) type(table)
      <class 'bs4.element.ResultSet'>
      (Pdb) len(table)
      2
      
      (Pdb) table[0]
      <div class="rightarea">\n<div class="information">...
      
      (Pdb) table[1]
      <div class="rightarea">\n<div class="spectitle">...
      
      (Pdb) table[0].find_all('div', class_='information')
      [<div class="information">\n<dl>\n<dt>\n<span><em>5,480</em>....
      

      所以,在你的脚本中,更新这一行应该让它工作。

      content_table1 = table[0].find_all('div', class_='information')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-17
        • 2018-11-07
        • 2019-06-20
        • 1970-01-01
        相关资源
        最近更新 更多