Q:

Python - is there a way to simplify the web scraping code?

trying to web-crape a property listing and got to  the below stage:
import bs4
from urllib.request import urlopen as Open
from urllib.request import Request
from bs4 import BeautifulSoup as soup

headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.3"}
my_url = "https://www.otodom.pl/oferta/narozne-2-pokoje-nowa-inwestycja-0-ID43FH9.html"
req = Request(url=my_url, headers=headers) 
html = Open(req).read() 

# get property features

container = page_soup.find("section", {"class":"section-overview"}).findNext("div").ul.findAll("li")

for contain in container:

    if "Rynek" in contain.text:
        rynek = contain.text.split(":")[1]
        break
    else:
        rynek = "N/A"

for contain in container:        
    if "Materiał budynku" in contain.text:
        material_budynku = contain.text.split(":")[1]
        break
    else:
        material_budynku = "N/A"

print(
rynek,
material_budynku,
        )

Each listing has a number of listed features, there's a total of over 40 unique features. In the code above I have just stated two examples, one of a present feature, and one of missing.
The end goal is for the web scrape to go through a list of all features, and check if they are present. If they are - take the feature variable, if they are not - put a generic "N/A".
I could potentially just do 40+ iterations of below...
for contain in container:        
    if "Materiał budynku" in contain.text:
        material_budynku = contain.text.split(":")[1]
        break
    else:
        material_budynku = "N/A"

... but I wonder whether there's a neater way?

A:

How about this?
features = []
for feature in ["Rynek", "Materiał budynku"]:
    for contain in container:
        if feature in contain.text:
            features.append(contain.text.split(":")[1])
            break
    else:  # if we didn't break
        features.append("N/A")

print(*features)

Instead of listing the features one by one, we simply iterate over them. The only slightly odd thing we have to do is use the little-known else branch of for-loops, with which we can test if we found a feature at all.

