Reputation: 57
<div id="hide-editing-34536258">1/2 and 2/1 are reciprocals.</div>
This is the tag I wanna scrape and I wanna print 1/2 and 2/1 are reciprocals.
I'll print it through get_text()
but I don't know how I scrape the tag.
I can do this.
find_all({"class":"hide-editing-3453658"}
but there are more tags to scrape and they have different numbers after 'high-editing-'
and I can't find any rules in the numbers.
Can anyone help me?
Upvotes: 0
Views: 49
Reputation: 17408
The attribute is id
not class
and you have give the tag you are looking in the find_all
method. You can use regex
to find all elements with certain pattern.
In [61]: import re
In [62]: a = """ <div id="hide-editing-34536258">1/2 and 2/1 are reciprocals.</div>
...: <div id="hide-editing-345258">1/4 and 2/1 are reciprocals.</div>
...: <div id="hide-editing-346258">1/5 and 2/1 are reciprocals.</div>
...: """
In [63]: soup = BeautifulSoup(a, "html.parser")
In [64]: all_divs = dates = soup.findAll("div", {"id" : re.compile('hide-editing.*')})
In [65]: all_divs
Out[65]:
[<div id="hide-editing-34536258">1/2 and 2/1 are reciprocals.</div>,
<div id="hide-editing-345258">1/4 and 2/1 are reciprocals.</div>,
<div id="hide-editing-346258">1/5 and 2/1 are reciprocals.</div>]
In [66]: [i.text.strip() for i in all_divs]
Out[66]:
['1/2 and 2/1 are reciprocals.',
'1/4 and 2/1 are reciprocals.',
'1/5 and 2/1 are reciprocals.']
Upvotes: 1
Reputation: 1
Maybe you can try with regular expression?
import re
text = '<div id="hide-editing-34536258">1/2 and 2/1 are reciprocals.</div>'
parsedText=re.findall('>([^<]+)', text)
print(parsedText[0])
Upvotes: 0