繁体   English   中英

我如何从中抓取文字<script> tags with bs4?

[英]How do I scrape text from <script> tags with bs4?

我正在尝试使用BS4从标记中抓取一些文本,但是每次运行脚本时,我都会不断收到TypeError。

我尝试使用几个不同的解析器,但它们都返回相同的TypeError。

我的python代码是:

s = requests.Session()
r = (s.get(url, headers=headers))
soup = BeautifulSoup(r.content, 'html5lib')
profile = soup.find('script', attrs={'name': 'window.profile'})['value']

我要抓取的HTML是:

<script>
// Profile helper.
window.profile = 'PROFILEIDHERE';
</script>

我的代码的预期结果是将'window.profile'的值分配给变量'profile',但是每次运行脚本时都会收到TypeError。

您可以使用get_text()获取标签的文本值:

allScripts = soup.find_all("script")
for script in allScripts:
    scriptText = script.get_text()
    scriptTextValue = scriptText.split("'")[1]
    print(scriptTextValue)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM