2

I have the a bunch of html data looking like this:

<div id="productplacementlist_1" class="c-channel-placement-content">

<h3 class="c-subheading-6">TITLE1 EXAMPLE</h3>

The "div id" is unique for every class, the value behind the "_" changes to 2, 3, 4, etc. I want the content (string, text) for every h3 class. So I use:

start = soup.find("h3").string

This works great for me, but only for the first "h3" item - when I print it out, I get the content, e.g. in this case:

Output: "TITLE 1 EXAMPLE"

I have 191 "strings" from h3 tags I want to save, each in a single value. So "start" is my starting point, from there, I jump to the next item with:

s2 = start.find_next("h3").string

This works great, I get the next item. But I think it's not the best way to manually create a new "start point" (like s2 here) for 191 items.

So I simply want a loop, starting from "start", then jump to s2, save this (automatically in the background) as s3, then jump to the next item and so on - the loop should stop on the last h3 tag.

OR

Other way is to get all h3 tags with one search, with the following command:

search2 = soup.find_all("h3", {"class": "c-subheading-6"})

Output:

<h3 class="c-subheading-6>"TITLE 1 EXAMPLE</h3>
<h3 class="c-subheading-6>"TITLE 2 EXAMPLE</h3>

This would be okay too, but I need to find a way to store the string (text only) of every h3 class in a single value, not in a list.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • Are you asking how you can assign a bunch of different strings to a variable without writing `var_name = mystring` a whole bunch of times? Especially if you don't know in advance how many there might be? – wwii Jul 13 '20 at 22:11
  • `h3s = [h3.string for h3 in soup.find_all("h3", {"class": "c-subheading-6"})]` – codedawi Jul 13 '20 at 22:13
  • Does this answer your question? [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – wwii Jul 13 '20 at 22:15
  • This is also equivalent - `h3s = [h3.string for h3 in soup.find_all("h3", "class_="c-subheading-6")]` – bigbounty Jul 13 '20 at 22:39

1 Answers1

0

SOLUTION:

h3s = [h3.string for h3 in soup.find_all("h3", {"class": "c-subheading-6"})] – 

from codedawi

Thank you some much, one line and I try 10.000 different things with 10+ lines of code. Thank you so much!

@wwii

Not directly, but it will help in the future (How do I create a variable number of variables?)

Comment: Are you asking how you can assign a bunch of different strings to a variable without writing var_name = mystring a whole bunch of times? Especially if you don't know in advance how many there might be? - Yes, woould be great to know that.

Thank you too!! You both helped me very much :)