擅长:python、mysql、java
<p>您可以确定行的长度,并相应地将文本解析为变量。见下面的例子</p>
<pre><code>num = 267
location_address = soup.select_one(f"[data-id='{num}'] .heading:contains('Address') + p")
print(location_address)
#Determine the number of address lines
print(len(location_address.find_all('br')))
TotalLines = len(location_address.find_all('br'))
line1 =''
line2=''
if TotalLines >1:
line1 = location_address.contents[0]
line2 = location_address.contents[2]
else:
line1 = location_address.contents[0]
print('Address Line1:',line1)
print('Address Line2:',line2)
</code></pre>
<p>输出:</p>
<p>地址行1:
铁杉路2号邮政信箱904
地址行2:
角溪,NL</p>