从脚本标签获取信息(WebScrap)

时间:2019-07-27 12:10:39

标签: python html beautifulsoup

#Python Code
from bs4 import BeautifulSoup
import urllib3

url ='https://www. SomeData .com'
req = urllib3.PoolManager()
res = req.request('GET', url)
soup = BeautifulSoup(res.data, 'html.parser')
res = soup.find_all('script')
print(res)

然后我得到了这样的东西:

Results below:
[
  <script>
        AAA.trackData.taxonomy = {
              a:"a",
              b:"b",
              c:"c2,
              ...} ;
</script>
</script>, <script async="" src="https://someData.com/js/detail.0a6eca28.js"></script>
]

如何将其转换为json格式以处理标签内的数据。

1 个答案:

答案 0 :(得分:0)

请检查是否有帮助。

script = soup.find('script', text=re.compile('AAA\.trackData\.taxonomy'))
json_text = re.search(r'^\s*AAA\.trackData\.taxonomy\s*=\s*({.*?})\s*;\s*$',
                      script.string, flags=re.DOTALL | re.MULTILINE).group(1)
data = json.loads(json_text)