簡體   English   中英

如何在python中的html源中解析javascript代碼?

[英]How to parse javascript code in html source in Python?

我正在嘗試通過Web抓取HTML源中JavaScript標記內的一些數據。

情況:我可以找到相應的<script></script>標記。 但是在該標簽內,有一個很大的字符串,需要將其轉換然后進行解析,這樣我才能獲得所需的精確數據。

問題是:我不知道該怎么做,也找不到一個明確而令人滿意的答案。

這是代碼:

我的目標是獲取此數據: "xe7fd4c285496ab91" ,它是內容的標識號,也稱為"contentId"

import requests
import bs4
import re

url = 'https://www.khanacademy.org/computing/computer-programming/programming/drawing-basics/pt/making-drawings-with-code'
response = requests.get(url)
soup = bs4.BeautifulSoup(response.text,'html.parser') # by the way I am not sure if this is the right way to parse the link

item = soup.find(string=re.compile('contentId')) # with this line I can get directly to the exact javascript tag that I need

print(item) # but as you can see, it's a pretty big string, and I need to parse it to get the desired data. But you can find that the desired data "xe7fd4c285496ab91" is in it.

我嘗試使用json.parse()但無法正常工作:

import json
jsonparsed=json.parse(item)

得到這個錯誤:

AttributeError: 'NavigableString' object has no attribute 'json'

我的問題是:如何獲得所需的數據? 有將字符串轉換為javascript的函數,以便我可以解析它嗎? 還是將此字符串轉換為JSON文件的方法?

(請記住,我將在具有類似HTML / JavaScript的多個鏈接上執行此操作)。

您可以只對文本使用正則表達式,而無需搜索腳本

import re
import requests

r = requests.get('https://www.khanacademy.org/computing/computer-programming/programming/drawing-basics/pt/making-drawings-with-code')
p = re.compile(r'contentId":"((?:(?!").)*)')  
i = p.findall(r.text)[0]
print(i)

正則表達式

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM