【发布时间】:2021-08-08 09:53:21
【问题描述】:
我想使用漂亮的汤从给定的脚本或网站上的任何 id 中提取 productId 值(186852001461)。
<script type="text/javascript">
/* <![CDATA[ */
var bv_single_product = {"prodname":"Honey Graham Gelato","productId":"186852001461"};
/* ]]> */
</script>
我的代码
import re
import requests
from bs4 import BeautifulSoup
final = "https://www.talentigelato.com/products/honey-graham-gelato"
response = requests.get(final, timeout=35)
soup = BeautifulSoup(response.content, "html.parser")
s = soup.findAll('script',attrs={'type': 'text/javascript'} )[17]
print(type(s))
html_content = str(s)
html_content = s.prettify()
print(html_content))
【问题讨论】:
标签: web-scraping beautifulsoup cdata