Python: как получить доступ к списку элементов класса div и перебрать их с помощью (BeautifulSoup) - PullRequest
0 голосов
/ 06 июня 2018

Я анализирую данные о производстве автомобилей с BeautifulSoup (см. Также мой первый вопрос ):

from bs4 import BeautifulSoup
import string

html = """
<h4>Production Capacity (year)</h4>
    <div class="profile-area">
      Vehicle 1,140,000 units /year
    </div>
<h4>Output</h4>
    <div class="profile-area">
      Vehicle 809,000 units ( 2016 ) 
    </div>
    <div class="profile-area">
      Vehicle 815,000 units ( 2015 ) 
    </div>
    <div class="profile-area">
      Vehicle 836,000 units ( 2014 ) 
    </div>
    <div class="profile-area">
      Vehicle 807,000 units ( 2013 ) 
    </div>
    <div class="profile-area">
      Vehicle 760,000 units ( 2012 ) 
    </div>
    <div class="profile-area">
      Vehicle 805,000 units ( 2011 ) 
    </div>
"""
soup = BeautifulSoup(html, 'lxml')

for item in soup.select("div.profile-area"):
  produkz = item.text.strip()
  produkz = produkz.replace('\n',':')

  prev_h4 = str(item.find_previous_sibling('h4'))
  if "Models" in prev_h4:
    models=produkz
  else:
    models=""

  if "Capacity" in prev_h4:
    capacity=produkz
  else:
    capacity=""

  if "( 2015 )" in produkz:
    prod15=produkz
  else:
    prod15=""
  if "( 2016 )" in produkz:
    prod16=produkz
  else:
    prod16=""
  if "( 2017 )" in produkz:
    prod17=produkz
  else:
    prod17=""

  print(models+';'+capacity+';'+prod15+';'+prod16+';'+prod17)

Моя проблема в том, что следующий цикл для всех соответствующих HTML-вхождений («div.profile-area») перезаписывает мой результат:

;Vehicle 1,140,000 units /year;;;;;;
;;;;;;Vehicle 809,000 units ( 2016 );
;;;;;Vehicle 815,000 units ( 2015 );;
;;;;Vehicle 836,000 units ( 2014 );;;
;;;Vehicle 807,000 units ( 2013 );;;;
;;Vehicle 760,000 units ( 2012 );;;;;
;;;;;;;

Мой желаемый результат:

;Vehicle 1,140,000 units /year;Vehicle 760,000 units ( 2012 );Vehicle 807,000 units ( 2013 );Vehicle 836,000 units ( 2014 );Vehicle 815,000 units ( 2015 );Vehicle 809,000 units ( 2016 );

Я был бы рад, если бы вы могли показать мне лучший способ структурировать мойкод.Заранее спасибо.

Ответы [ 2 ]

0 голосов
/ 07 июня 2018

Это мое решение. Вам нужно позаботиться о каждом элементном теге и разобрать его соответствующим образом.Я пошел дальше к вашей проблеме и предложил более гибкий способ доступа к каждому значению данных.надеюсь, это поможет.

import re

from bs4 import BeautifulSoup

html_doc = """
<h4>Production Capacity (year)</h4>
    <div class="profile-area">
    Vehicle 1,140,000 units /year
    </div>
<h4>Output</h4>
    <div class="profile-area">
    Vehicle 809,000 units ( 2016 ) 
    </div>
    <div class="profile-area">
    Vehicle 815,000 units ( 2015 ) 
    </div>
    <div class="profile-area">
    Vehicle 836,000 units ( 2014 ) 
    </div>
    <div class="profile-area">
    Vehicle 807,000 units ( 2013 ) 
    </div>
    <div class="profile-area">
    Vehicle 760,000 units ( 2012 ) 
    </div>
    <div class="profile-area">
    Vehicle 805,000 units ( 2011 ) 
    </div>"""

soup = BeautifulSoup(html_doc, 'html.parser')
h4_elements = soup.find_all('h4')
profile_areas = soup.find_all('div', attrs={'class': 'profile-area'})
print('\n')
print("++++++++++++++++++++++++++++++++++++")
print("Element counts")
print("++++++++++++++++++++++++++++++++++++")
print("Total H4: {}".format(len(h4_elements)))
print("++++++++++++++++++++++++++++++++++++")
print("Total profile-area: {}".format(len(profile_areas)))
print("++++++++++++++++++++++++++++++++++++")
print('\n')

for i in h4_elements:
    print("++++++++++++++++++++++++++++++++++++")
    print(i.text.rstrip().lstrip())
    print("++++++++++++++++++++++++++++++++++++")
    del profile_areas[0]
    for j in profile_areas:
        raw = re.sub('[^A-Za-z0-9]+', ' ', j.text.replace(',','').lstrip().rstrip())
        raw = raw.rstrip()
        el = raw.split(' ')

        print('Type: {} '.format(el[0]))
        print('Sold: {} {} '.format(el[1], el[2]))
        print('Year: {} '.format(el[3]))
        print("++++++++++++++++++++++++++++++++++++")

Вывод следующий:

 ++++++++++++++++++++++++++++++++++++
Production Capacity (year)
++++++++++++++++++++++++++++++++++++
Type:Vehicle 
Sold: 809000 units 
Year: 2016 
++++++++++++++++++++++++++++++++++++
Type:Vehicle 
Sold: 815000 units 
Year: 2015 
++++++++++++++++++++++++++++++++++++
Type:Vehicle 
Sold: 836000 units 
Year: 2014 
++++++++++++++++++++++++++++++++++++
Type:Vehicle 
Sold: 807000 units 
Year: 2013 
++++++++++++++++++++++++++++++++++++
Type:Vehicle 
Sold: 760000 units 
Year: 2012 
++++++++++++++++++++++++++++++++++++
Type:Vehicle 
Sold: 805000 units 
Year: 2011 
++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++
Output
++++++++++++++++++++++++++++++++++++
Type:Vehicle 
Sold: 815000 units 
Year: 2015 
++++++++++++++++++++++++++++++++++++
Type:Vehicle 
Sold: 836000 units 
Year: 2014 
++++++++++++++++++++++++++++++++++++
Type:Vehicle 
Sold: 807000 units 
Year: 2013 
++++++++++++++++++++++++++++++++++++
Type:Vehicle 
Sold: 760000 units 
Year: 2012 
++++++++++++++++++++++++++++++++++++
Type:Vehicle 
Sold: 805000 units 
Year: 2011 
++++++++++++++++++++++++++++++++++++
0 голосов
/ 06 июня 2018

Я бы посоветовал вам сохранить каждую запись в словаре, затем вы можете легко извлечь нужные поля в конце (вы, кажется, не хотите 2011?):

from bs4 import BeautifulSoup
import re

html = """
<h4>Production Capacity (year)</h4>
    <div class="profile-area">
      Vehicle 1,140,000 units /year
    </div>
<h4>Output</h4>
    <div class="profile-area">
      Vehicle 809,000 units ( 2016 ) 
    </div>
    <div class="profile-area">
      Vehicle 815,000 units ( 2015 ) 
    </div>
    <div class="profile-area">
      Vehicle 836,000 units ( 2014 ) 
    </div>
    <div class="profile-area">
      Vehicle 807,000 units ( 2013 ) 
    </div>
    <div class="profile-area">
      Vehicle 760,000 units ( 2012 ) 
    </div>
    <div class="profile-area">
      Vehicle 805,000 units ( 2011 ) 
    </div>
"""

soup = BeautifulSoup(html, 'lxml')
units = {}

for item in soup.find_all(['h4', 'div']):
    if item.name == 'h4':
        for h4 in ['capacity', 'output', 'models']:
            if h4 in item.text.lower():
                break
    elif item.get('class', [''])[0] == 'profile-area':
        vehicle = item.get_text(strip=True)

        if h4 == 'output':
            re_year = re.search(r'\( (\d+) \)', vehicle)

            if re_year:
                year = re_year.group(1)
            else:
                year = 'unknown'

            units[year] = vehicle
        else:
            units[h4] = vehicle

req_fields = ['models', 'capacity', '2012', '2013', '2014', '2015', '2016']            
print(';'.join([units.get(field, '') for field in req_fields]))

Это отобразит:

;Vehicle 1,140,000 units /year;Vehicle 760,000 units ( 2012 );Vehicle 807,000 units ( 2013 );Vehicle 836,000 units ( 2014 );Vehicle 815,000 units ( 2015 );Vehicle 809,000 units ( 2016 )

Регулярное выражение используется для извлечения года из записи транспортного средства.Затем он используется в качестве ключа в словаре.

Для HTML в pastebin это дает:

Volkswagen Golf, Golf Variant(Estate), Golf Plus, CrossGolf (2006-), e-Golf (2014-)Volkswagen Touran, CrossTouran (2007-), Tiguan (2007-);I.D. electric vehicles based on MEB (planning);SEAT new SUV MQB-A2 platform (2018- planning);Components:press shop, chassis, plastics technology;Vehicle 1,140,000 units /year;Vehicle 760,000 units ( 2012 );Vehicle 807,000 units ( 2013 );Vehicle 836,000 units ( 2014 );Vehicle 815,000 units ( 2015 );Vehicle 809,000 units ( 2016 )
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...