Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.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
在BeautifulSoup Python中从脚本标记查找数据_Python_Web Scraping_Beautifulsoup_Python Requests - Fatal编程技术网

在BeautifulSoup Python中从脚本标记查找数据

在BeautifulSoup Python中从脚本标记查找数据,python,web-scraping,beautifulsoup,python-requests,Python,Web Scraping,Beautifulsoup,Python Requests,我正在制作一个webscraper,但遇到了一个问题。如何获取该数据值 <script> var store = { data: 'ffggel4784hth4ve8bf5hhe8rh4b1d4g84usd9', domain: 'www.domain.com' }; </script> 我得到了输出

我正在制作一个webscraper,但遇到了一个问题。如何获取该数据值

<script>
                var store = {
                        data: 'ffggel4784hth4ve8bf5hhe8rh4b1d4g84usd9',
                        domain: 'www.domain.com'
                };
</script>
我得到了输出

                var store = {
                        data: 'ffggel4784hth4ve8bf5hhe8rh4b1d4g84usd9',
                        domain: 'www.domain.com'
                };

现在如何获取数据值。

这是普通字符串,所以使用字符串的函数,如
strip()
split()
replace()
,切片

比如说

text = '''var store = {
                        data: 'ffggel4784hth4ve8bf5hhe8rh4b1d4g84usd9',
                        domain: 'www.domain.com'
                };'''

lines = text.split('\n')

parts = lines[1].strip().split(': ')
name = parts[0]
data = parts[1].strip(',')[1:-1]
print(name, '=', data)

parts = lines[2].strip().split(': ')
name = parts[0]
data = parts[1].strip(',')[1:-1]
print(name, '=', data)
结果

data = ffggel4784hth4ve8bf5hhe8rh4b1d4g84usd9
domain = www.domain.com

这回答了你的问题吗?它是普通字符串,所以将其拆分为行-
split('\n”)
并在
上拆分:
或使用
[start:end]
进行切片。您也可以使用
regex
data = ffggel4784hth4ve8bf5hhe8rh4b1d4g84usd9
domain = www.domain.com