Extraje datos de un sitio web, terminé con una lista que incluye la etiqueta span y los datos que necesito, probé algunos ajustes pero no puedo encontrar el método adecuado. Quiero eliminar la etiqueta span y recuperar solo el nombre y la información del móvil.

[<span class="a-size-medium a-color-base a-text-normal">Huawei Mate SE Factory Unlocked 5.93” - 4GB/64GB Octa-core Processor| 16MP + 2MP Dual Camera| GSM Only |Grey (US Warranty)</span>, <span class="a-size-medium a-color-base a-text-normal">Huawei Mate SE Factory Unlocked 5.93” - 4GB/64GB Octa-core Processor| 16MP + 2MP Dual Camera| GSM Only |Grey (US Warranty)</span>, <span class="a-size-medium a-color-base a-text-normal">Huawei Mate SE Factory Unlocked 5.93” - 4GB/64GB Octa-core Processor| 16MP + 2MP Dual Camera| GSM Only |Grey (US Warranty)</span>, <span class="a-size-medium a-color-base a-text-normal">Huawei Honor 8X (64GB + 4GB RAM) 6.5" HD 4G LTE GSM Factory Unlocked Smartphone - International Version No Warranty JSN-L23 (Black)</span>, <span class="a-size-medium a-color-base a-text-normal">Huawei Honor 8X (64GB + 4GB RAM) 6.5" HD 4G LTE GSM Factory Unlocked Smartphone - International Version No Warranty JSN-L23 (Black)</span>]

Quiero salida de la siguiente manera:

[ Huawei Mate SE Factory Unlocked 5.93” - 4GB/64GB Octa-core Processor| 16MP + 2MP Dual Camera| GSM Only |Grey,Huawei Mate SE Factory Unlocked 5.93” - 4GB/64GB Octa-core Processor| 16MP + 2MP Dual Camera| GSM Only |Grey (US Warranty),Huawei Honor 8X (64GB + 4GB RAM) 6.5" HD 4G LTE GSM Factory Unlocked Smartphone - International Version No Warranty JSN-L23 (Black)]

La lista anterior solo contiene algunos elementos de mi lista principal. Eliminaré varias entradas más tarde.

1
Arpit Vasava 11 may. 2019 a las 11:06

3 respuestas

La mejor respuesta

Extraiga cada elemento de la lista y cárguelo en BeautifulSoup, seleccione todas las etiquetas de extensión. Si tiene una lista real de cadenas, espero que encierre cadenas. Agregar a un conjunto para eliminar duplicados.

from bs4 import BeautifulSoup as bs

aList = ['<span class="a-size-medium a-color-base a-text-normal">Huawei Mate SE Factory Unlocked 5.93” - 4GB/64GB Octa-core Processor| 16MP + 2MP Dual Camera| GSM Only |Grey (US Warranty)</span>, <span class="a-size-medium a-color-base a-text-normal">Huawei Mate SE Factory Unlocked 5.93” - 4GB/64GB Octa-core Processor| 16MP + 2MP Dual Camera| GSM Only |Grey (US Warranty)</span>, <span class="a-size-medium a-color-base a-text-normal">Huawei Mate SE Factory Unlocked 5.93” - 4GB/64GB Octa-core Processor| 16MP + 2MP Dual Camera| GSM Only |Grey (US Warranty)</span>, <span class="a-size-medium a-color-base a-text-normal">Huawei Honor 8X (64GB + 4GB RAM) 6.5" HD 4G LTE GSM Factory Unlocked Smartphone - International Version No Warranty JSN-L23 (Black)</span>, <span class="a-size-medium a-color-base a-text-normal">Huawei Honor 8X (64GB + 4GB RAM) 6.5" HD 4G LTE GSM Factory Unlocked Smartphone - International Version No Warranty JSN-L23 (Black)</span>']
for i in aList:
    soup = bs(i, 'lxml')
    text = [item.text for item in soup.select('span')]  #list
    print(text)
    text = ','.join([item.text for item in soup.select('span')]) #comma separated string
    print(text)
0
QHarr 11 may. 2019 a las 09:38
s = 'Huawei Mate SE Factory Unlocked 5.93” - 4GB/64GB Octa-core Processor| 16MP + 2MP Dual Camera| GSM Only |Grey (US Warranty),Huawei Mate SE Factory Unlocked 5.93” - 4GB/64GB Octa-core Processor| 16MP + 2MP Dual Camera| GSM Only |Grey (US Warranty),Huawei Mate SE Factory Unlocked 5.93” - 4GB/64GB Octa-core Processor| 16MP + 2MP Dual Camera| GSM Only |Grey (US Warranty), Huawei Honor 8X (64GB + 4GB RAM) 6.5" HD 4G LTE GSM Factory Unlocked Smartphone - International Version No Warranty JSN-L23 (Black), Huawei Honor 8X (64GB + 4GB RAM) 6.5" HD 4G LTE GSM Factory Unlocked Smartphone - International Version No Warranty JSN-L23 (Black)'

sp = s.split(",")
gl = []

for sk in sp:   
    gl.append(sk)

from collections import OrderedDict    
res = list(OrderedDict.fromkeys(gl))

print (res)

(Como cada elemento se repite después de a, lo usé como separador. Asegúrese de que, después de todo, la alineación sea correcta) Espero que ayude

0
j.doe 12 may. 2019 a las 05:06

¿Te refieres a algo como esto?

txt = "<span class=\"a-size-medium a-color-base a-text-normal\">Huawei Mate SE Factory Unlocked 5.93” - 4GB/64GB Octa-core Processor| 16MP + 2MP Dual Camera| GSM Only |Grey (US Warranty)</span>, <span class=\"a-size-medium a-color-base a-text-normal\">Huawei Mate SE Factory Unlocked 5.93” - 4GB/64GB Octa-core Processor| 16MP + 2MP Dual Camera| GSM Only |Grey (US Warranty)</span>, <span class=\"a-size-medium a-color-base a-text-normal\">Huawei Mate SE Factory Unlocked 5.93” - 4GB/64GB Octa-core Processor| 16MP + 2MP Dual Camera| GSM Only |Grey (US Warranty)</span>, <span class=\"a-size-medium a-color-base a-text-normal\">Huawei Honor 8X (64GB + 4GB RAM) 6.5\" HD 4G LTE GSM Factory Unlocked Smartphone - International Version No Warranty JSN-L23 (Black)</span>, <span class=\"a-size-medium a-color-base a-text-normal\">Huawei Honor 8X (64GB + 4GB RAM) 6.5\" HD 4G LTE GSM Factory Unlocked Smartphone - International Version No Warranty JSN-L23 (Black)</span>"
x = txt.split(", ")
mylist = list(dict.fromkeys(x))
list = []
for val in mylist:
    if ("</span>" in val):
        val = val[:val.rfind("</span>")]
    if ("<span class=\"a-size-medium a-color-base a-text-normal\">" in val):
        val = val[len("<span class=\"a-size-medium a-color-base a-text-normal\">"):]

    list.append(val)
1
mopebi 28 may. 2019 a las 07:45