У меня есть веб-скребок, который очищает страницы со многими разделами информации, используя bs4. Так как многие разделы повторяют div class
, это трудно очистить. Я пытаюсь найти способ заставить его начать поиск lxml после определенной фразы в html. Есть ли способ сделать это?
ниже приведен небольшой пример того, с чем я работаю, пытаясь получить что-то вроде table_soup
, чтобы начать после определенной фразы.
from bs4 import BeautifulSoup
import csv
import re
# Making get request
r = requests.get('https://m.the-numbers.com/movie/Black-Panther')
# Creating BeautifulSoup object
soup = BeautifulSoup(r.text, 'lxml')
# Localizing table from the BS object
table_soup = soup.find('div', class_='row').find('div', class_='table-responsive').find('table', id='movie_finances')
website = 'https://m.the-numbers.com/'
# Iterating through all trs in the table except the first(header) and the last two(summary) rows
for tr in table_soup.find_all('tr')[1:6]:
tds = tr.find_all('td')
title = tds[0].text.strip()
# make sure that home market performance doesnt check the second one
if title != 'Home Market Performance':
details.append({
'title': title,
'amount': tds[1].text.strip(),
})
summary_soup = soup.find('div', id='summary').find('div', class_='table-responsive').find('table', class_='table table-sm')
summaryList = []
for tr in summary_soup.find_all('tr')[1:4]:
tdmd = tr.find_all('td')
summaryList.append({
'unit': tdmd[1].text.strip(),
})```