Python中脚本标记中的Web抓取
我使用Python中脚本标记中的Web抓取,python,json,selenium,web-scraping,beautifulsoup,Python,Json,Selenium,Web Scraping,Beautifulsoup,我使用BeautifulSoup获得了脚本标记。然后我变成了json对象。我想要的信息在数据['x']中,但它被卡在b标签之间。 例如: <b>infoiwant</b><br>NA<br>infoinwant</br>columniwant: 123','<b>infoiwant</b><br>NA<br>columniwant: 123'</br>columniwant:
BeautifulSoup
获得了脚本标记。然后我变成了json
对象。我想要的信息在数据['x']
中,但它被卡在b标签之间。
例如:
<b>infoiwant</b><br>NA<br>infoinwant</br>columniwant: 123','<b>infoiwant</b><br>NA<br>columniwant: 123'</br>columniwant: 123
infoiwant
NA
infoinwantcolumniwant:123','infoiwant
NA
columniwant:123'columniwant:123'
在转换为json之前,我如何从这些b元素中获取信息,您可以使用BeautifulSoup get_text()方法吗?也许像
soup.find('b').get_text()
在转换为json之前,可以使用BeautifulSoup get_text()方法吗?也许像
soup.find('b').get_text()
从
标签中提取数据的一种方法是使用re
模块:
import re
from bs4 import BeautifulSoup
html_text = """
<script>
var data['x'] = '<b>infoiwant</b><br>NA<br>infoinwant</br>columniwant: 123';
</script>
"""
html_data = re.search(r"data\['x'\] = '(.*?)';", html_text).group(1)
soup = BeautifulSoup(html_data, "html.parser")
print(soup.find("b").get_text(strip=True))
从
标签中提取数据的一种方法是使用re
模块:
import re
from bs4 import BeautifulSoup
html_text = """
<script>
var data['x'] = '<b>infoiwant</b><br>NA<br>infoinwant</br>columniwant: 123';
</script>
"""
html_data = re.search(r"data\['x'\] = '(.*?)';", html_text).group(1)
soup = BeautifulSoup(html_data, "html.parser")
print(soup.find("b").get_text(strip=True))
.find_all('b')应足以获取b标签。br对于其他..find_all('b')应该足以获得b标签。br表示其他。和来自html中的脚本标记。因此,使用findbmethod不适用于meThe,而是来自html中的脚本标记。所以使用findbethod对我不起作用