Соскрести текст со 2-го <BR> - PullRequest
       4

Соскрести текст со 2-го <BR>

0 голосов
/ 06 октября 2018

У меня есть html выдержка ниже, обратите внимание, повторение двух тд для каждой строки, которую мне нужно захватить.

<table class="ent">
<tbody class=""><tr class="tablestyle">

    <td class="hide_on_mobile">  <a href="../" class="">
        <img class="ProductImage" src="https://.."></a>
    </td>
    <td class="hide_on_mobile" align="center">
        <strong class="">
            <span style="font-size:1.4em;" class="">Scraped okay - col0</span>
                <br>
                <br>Scrape this text - col1</strong><br>
                <br><i><span style="color:indigo;" class="">Scrape this text - col2
                <br class="">
                <br>Next Event: Scrape this text -col3</span></i>
    </td>

Мне нужно захватить 4 разных фрагмента данных col0, col1, col2, col3

У меня уже работает col0.Мне нужно захватить col1, col2, col3

Я пытаюсь использовать BR, то есть после span

взять текст после 2-го BR после col1

взять текстпосле 3-го BR после для col2

взять текст после 5-го BR после для col3

Я не могу заставить col1 работать с br> br.Любые идеи, как я могу решить это?

import sqlite3
import datetime
import requestsnt
import pandas as pd
from bs4 import BeautifulSoup

url = "http:/*"

r = requests.get(url)
source = r.text
t = datetime.datetime.now().date()
soup = BeautifulSoup(source, "lxml")

row_count=200

row_marker = 0

new_table = pd.DataFrame(columns = ["col0", "col1", "col2","col3", "DateAdded"], index = range(0,row_count)) # I don't know the number of rows

# For col0
column_marker = 0
for layout in soup.select("strong > span"):
            new_table.iat[row_marker,column_marker] = layout.text.strip()
            new_table.iat[row_marker,4] = t
            row_marker +=1

# For col 1

column_marker = 1
row_marker = 0
for layout in soup.select("strong > span > br > br"):
            new_table.iat[row_marker,column_marker] = layout.text.strip()
            row_marker +=1

1 Ответ

0 голосов
/ 06 октября 2018
#since you said there are multiple trs
trs = data.find_all('tr')


for tr in trs:
    l = []
    td =  tr.find_all('td')
    #since first td will never have data.. acc to the above posted ques 
    for tags in td[1]:
        try:
            if tags.text:
                print(tags.text)
                l.extend((tags.text).split('\n'))
        except:
            pass

#once there are more trs keep below code inside the loop
#then store the data in a df..since each loop will give new list
str_data = [' '.join(s.split()) for s in l if s]        
str_data.remove('')
print(str_data)

Выход

['Scraped okay - col0',
 'Scrape this text - col1',
 'Scrape this text - col2',
 'Next Event: Scrape this text -col3']
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...