需要的爬取的内容隐藏在script中,怎么提取?

隐藏在这里

需要的内容在后面

网页源码已爬取,现需要提取标签内特定内容。新手小白,还请多多指点!

方法一,用正则模块re提取你需要的内容

方法二,用正则获取到整个script,使用python模块pyexecjs运行script,得到__SEARCH_RESULT__的变量的值

pyexecjs的使用方法看这个文章

https://www.cnblogs.com/woaixuexi9999/p/9419488.html

import requests

from bs4 import BeautifulSoup

import re

import json

req=requests.get(url,headers=headers)

soup=BeautifulSoup(req.text,'lxml')

r=soup.select('script:contains("window.__SEARCH_RESULT__")')[0]

res = re.findall('window.__SEARCH_RESULT__ = (.*);',r.string)

data=json.loads(res[0])

print(data)

print(type(data))#转换成了字典,从中解析中所需数据。