Reputation: 6554
I need to find element with 'random' id in html. My code is look like:
from bs4 import BeautifulSoup
import re
soup = BeautifulSoup(html)
print soup.find(id="id_123456_name")
123456
- may changes every time, so I find this, but I cant understand, how use it.
I try:
soup.find(id="id_%s_name" % (re.compile("\d+")) )
But nothing find. Whats the problem?
Upvotes: 0
Views: 90
Reputation: 1124278
You need to make the whole value a regular expression object:
soup.find(id=re.compile("id_\d+_name"))
In your version, you are still looking for a literal string, not a regular expression, because you converted the regular expression object into a string instead. The literal string has a very strange value:
>>> import re
>>> "id_%s_name" % (re.compile("\d+"))
'id_<_sre.SRE_Pattern object at 0x10f111750>_name'
This value of course is never found in your HTML document.
Upvotes: 1