Quintin De Kerpel
Quintin De Kerpel

Reputation: 23

How to print certain parts of python strings?

I am trying to webscrape a page off of amazon. The part where I am stuck is how I get the data in a neat fashion. Currently I have a list, called a, with every item on the webpage of amazon. My code is this:

import requests
from bs4 import BeautifulSoup

r = requests.get('https://www.amazon.de/s/ref=nb_sb_noss_2?__mk_nl_NL=    %C3%85M%C3%85%C5%BD%C3%95%C3%91&url=search-alias%3Daps&field-keywords=processor')

page = BeautifulSoup(r.text, 'html.parser')

a = []
itemAmount = (len(page.find_all('div', class_='s-item-container')))
for x in range(itemAmount):
a.append(page.find_all('div', class_='s-item-container')[x].get_text("|",     strip=True))

print(*a, sep='\n')

This outputs the name of the products in an array called a. It looks something like this:

Intel Core i7-8700K Processor (6x 3.7 GHz Taktfrequenz, 12 MB L3-Cache, Boxed ohne Kühler)|von|Intel|EUR 342,45|Prime|Kostenlose Lieferung möglich.|Andere Angebote|EUR 330,00|(102 gebrauchte und neue Artikel)|4,6 von 5 Sternen|110
AMD Ryzen 5 1600 Prozessor|von|AMD|EUR 144,90|Prime|Exklusiv für Prime-Mitglieder|Versandbereit in 1-2 Tagen.|Neueres Modell dieses Artikels anzeigen|4,6 von 5 Sternen|286
AMD FX-8350 FD8350FRHKBOX Prozessor (Black Edition, 8MB Cache, 4.00 GHz Turbo 4.20 GHz, Sockel AM3+) weiß|von|AMD|EUR 73,99|Prime|Kostenlose Lieferung möglich.|Andere Angebote|EUR 65,00|(55 gebrauchte und neue Artikel)|4,3 von 5 Sternen|356
Intel Core i5-7500 Prozessor (7. Generation, bis zu 3.80 GHz mit Intel Turbo-Boost-Technik 2.0, 6 MB Intel Smart-Cache)|von|Intel|EUR 184,99|Prime|Kostenlose Lieferung möglich.|Andere Angebote|EUR 177,63|(76 neue Artikel)|4,4 von 5 Sternen|30
AMD Ryzen 5 1400 Prozessor|von|AMD|EUR 117,90|Prime|Kostenlose Lieferung möglich.|Andere Angebote|EUR 109,82|(75 gebrauchte und neue Artikel)|4,6 von 5 Sternen|287
Intel Pentium G4400 BX80662G4400 Prozessor (3,30 GHz, 3 MB Intel Smart-Cache)|von|Intel|EUR 49,99|Prime|Kostenlose Lieferung möglich.|Andere Angebote|EUR 40,33|(88 gebrauchte und neue Artikel)|4,2 von 5 Sternen|19
Intel Core i9-7900X Prozessor, der X-Serie (bis zu 4,30 GHz, 13,75 MB Intel Cache|von|Intel|EUR 875,99|Prime|Kostenlose Lieferung möglich.|Nur noch 19 Stück auf Lager - jetzt bestellen.|Andere Angebote|EUR 569,00|(47 neue Artikel)|5 von 5 Sternen|8
Intel Core i7–7700 K 4,2 GHz QuadCore 8 MB Cache Prozessor|von|Intel|EUR 289,00|Prime|Kostenlose Lieferung möglich.|Nur noch 1 Stück auf Lager - jetzt bestellen.|Neueres Modell dieses Artikels anzeigen|4,1 von 5 Sternen|6
Intel Core i5-6400 2,7GHz Boxed CPU|von|Intel|EUR 160,99|Prime|Kostenlose Lieferung möglich.|Nur noch 1 Stück auf Lager - jetzt bestellen.|Andere Angebote|EUR 127,90|(64 gebrauchte und neue Artikel)|3,9 von 5 Sternen|20
AMD FD4300WMHKBOX Quad-Core Prozessor (3,8 GHz, AM3 + Sockel, 8 MB Cache, 95 Watt) mit Kühlkörper und Lüfter|von|AMD|EUR 49,99|Prime|Kostenlose Lieferung möglich.|Andere Angebote|EUR 42,11|(48 gebrauchte und neue Artikel)|Neueres Modell dieses Artikels anzeigen|3,9 von 5 Sternen|51
AMD Ryzen 7 1700 Prozessor mit Wraith-Spire-LED-Kühler|von|AMD|EUR 207,90|Prime|Kostenlose Lieferung möglich.|Andere Angebote|EUR 190,08|(82 gebrauchte und neue Artikel)|Neueres Modell dieses Artikels anzeigen|4,6 von 5 Sternen|286
Intel Core i5-7600K Prozessor der 7. Generation (bis zu 4.20 GHz mit Intel Turbo-Boost-Technik 2.0, 6 MB Intel Smart-Cache)|von|Intel|EUR 205,00|KOSTENFREIE Lieferung|Andere Angebote|EUR 198,09|(50 gebrauchte und neue Artikel)|Neueres Modell dieses Artikels anzeigen|4,2 von 5 Sternen|133
Intel Pentium G4560 3,50GHz Boxed CPU|von|Intel|EUR 56,99|Prime|Kostenlose Lieferung möglich.|Nur noch 12 Stück auf Lager - jetzt bestellen.|Andere Angebote|EUR 51,00|(54 neue Artikel)|4,5 von 5 Sternen|38
Intel Celeron Prozessor G3900 (2,80 GHz, 2 MB Intel Smart-Cache)|von|Intel|EUR 34,99|Prime|Kostenlose Lieferung möglich.|Andere Angebote|EUR 25,64|(63 gebrauchte und neue Artikel)|4,4 von 5 Sternen|28

Now my question is: How do i get the data out of this? I'm looking for the name and the price. So something like this:

Intel Core i7-8700K Processor | EUR 342,45
AMD Ryzen 5 1600 Prozessor | EUR 144,90

You get the gist. Is this even possible? I couldn't find it. Thanks alot!

Upvotes: 2

Views: 63

Answers (1)

Jean-François Fabre
Jean-François Fabre

Reputation: 140256

you can pass you list of strings to the csv module, then take only first and fourth entry of each row. Split first entry according to opening parenthesis:

cr = csv.reader(a,delimiter="|")
for row in cr:
    print("{} | {}".format(row[0].split("(")[0].strip(),row[3]))

result with your data:

Intel Core i7-8700K Processor | EUR 342,45
AMD Ryzen 5 1600 Prozessor | EUR 144,90
AMD FX-8350 FD8350FRHKBOX Prozessor | EUR 73,99
Intel Core i5-7500 Prozessor | EUR 184,99
AMD Ryzen 5 1400 Prozessor | EUR 117,90
Intel Pentium G4400 BX80662G4400 Prozessor | EUR 49,99
Intel Core i9-7900X Prozessor, der X-Serie | EUR 875,99
Intel Core i7–7700 K 4,2 GHz QuadCore 8 MB Cache Prozessor | EUR 289,00
Intel Core i5-6400 2,7GHz Boxed CPU | EUR 160,99
AMD FD4300WMHKBOX Quad-Core Prozessor | EUR 49,99
AMD Ryzen 7 1700 Prozessor mit Wraith-Spire-LED-Kühler | EUR 207,90
Intel Core i5-7600K Prozessor der 7. Generation | EUR 205,00
Intel Pentium G4560 3,50GHz Boxed CPU | EUR 56,99
Intel Celeron Prozessor G3900 | EUR 34,99

another method using csv writer for output only (but doesn't "space out" the fields, no space added before & after the separator):

cr = csv.reader(a,delimiter="|")
cw = csv.writer(sys.stdout,delimiter="|")
cw.writerows([row[0].split("(")[0].strip(),row[3]] for row in cr)

sys.stdout can be an opened file handle too.

The most "complex" part is this:

row[0].split("(")[0].strip()

this is just taking first column, splitting according to parenthese, taking the first part (the left part), and remove leading/trailing space around.

Upvotes: 2

Related Questions