Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中脚本标记中的Web抓取_Python_Json_Selenium_Web Scraping_Beautifulsoup - Fatal编程技术网

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
infoinwant
columniwant: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对我不起作用