我正在使用BeautifulSoup从网站上获取产品说明(带有html标签)。
我当前代码的问题是它无法获取某些产品网址的描述。就像下面产品2的示例一样。
请查看以下两个网址,以检查html标签在说明中的区别- product 1
我需要一种通用的方式来存储描述。
当前我的代码如下-
def get_soup(url):
try:
response = requests.get(url)
if response.status_code == 200:
html = response.content
return BeautifulSoup(html, "html.parser")
except Exception as ex:
print("error from " + url + ": " + str(ex))
def get_product_details(url):
try:
desc_list = soup.select('p ~ ul')
prod_details['description'] = ''.join([str(i) for i in desc_list])
return prod_details
except Exception as ex:
logger.warning('%s - %s', ex, url)
if __name__ == '__main__':
print("product1 description:")
get_product_details("http://www.aprisin.com.sg/p-748-littletikespoptunesguitar.html")
print("product2 description:")
get_product_details("http://www.aprisin.com.sg/p-1052-172083littletikesclassiccastle.html")
样本输出-
product1 description:
<ul>
<li>Freestyle</li>
<li>Play along with 5 pre-set tunes: </li>
</ul><ul>
<li>Each string will play a note</li>
<li>Guitar has a whammy bar</li>
<li>2-in-1 volume control and power button </li>
<li>Simple and easy to use </li>
<li>Helps develop music appreciation </li>
<li>Requires 3 "AA" alkaline batteries (included)</li>
</ul>
product2 description: