i'm searching for the text City immediately prior to the tag I want: the city and state string. Here is the html:
<b>City:</b>
<a href="/city/New-York-New-York.html">New York, NY</a>
here is the code:
zipCode = str(11021)
url = "http://www.city-data.com/zips/" + zipCode + ".html"
r = requests.get(url)
data = r.text
soup = BeautifulSoup(data)
main_body = soup.findAll(text="City:")
print main_body
All I get, however, are empty brackets. How do I search for the City: text and then get the string for the next tag?
You can use
next_elementsfrom the text node until you find an<a>tag and extract its text:Run it like (asumming
htmlfilehas the test data of the question):That yields: