我想从给定的脚本中提取productId值(186852001461(,或者使用漂亮的汤从网站上的任何id中提取。
<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))
您需要先使用.string
,然后使用regex
,以便将值转储到json.loads()
。
方法如下:
import json
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]
data = json.loads(re.search(r"single_product = ({.*})", s.string).group(1))
print(data["productId"])
输出:
186852001461