Split Two Tags And Append Them Separately In Bs4 Python
I have a
TR which is dynamic and I try to get it like this:
self.soup.select("#detail > tbody > tr > td:nth-of-type(2)")
I want all
td in it which they are dynamic in this way:
they may have only string or both string and
<a href> now I want to split the string in some variable and "string" of that
<a> tag in another but the thing that matter is that
td which has no
<a> I want it to append "None" because both variables should have the same length and index in order to "zip" them correctly for further use.
here is some example :
which when they are appending to var should look like this:
event = ["the text I want"] vessel = ["the other text i want"]
and another 'possible' td:
<td class='bolt'> "another string we need" </td>
and the final result :
event = ["the text I want","another string we need"] vessel = ["the other text i want", None(or empty),]
If there can be one or two text nodes (as described in question), you can use
vessel =  event =  for td in self.soup.select("#detail > tbody > tr > td:nth-of-type(2)"): event.append([i.strip() for i in td.strings if i.strip()]) vessel.append(([i.strip() for i in td.strings if i.strip()] + [None])) print(event) ['"the text I want"', '"another string we need"'] print(vessel) ['the other text i want', None]
Let me know in case there might be more complex cases
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module