python - 从脚本中抓取网页
问题描述
我正在尝试使用 python 提取公司使用的语言比例BeautifulSoup
。
然而,信息似乎来自脚本,而不是来自 HTML,我遇到了一些麻烦。
例如,从下一页,当我尝试
webpage ="https://www.zippia.com/amazon-com-careers-487/"
page = requests.get(webpage)
soup = BeautifulSoup(page.content, 'lxml')
for links in soup.find_all('div', {'class':'companyEducationDegrees'}):
raw_text = links.get_text()
lines = raw_text.split('\n')
print(lines)
print('-------------------')
我没有得到任何结果,而理想的结果应该是Spanish 61.1%, French 9,7%, etc
解决方案
正如您已经发现的那样,数据是通过 JS 放入页面的。但是,您仍然可以获取该数据,因为公司上的全部数据始终与页面一起加载。requests
您可以通过++ (+ BeautifulSoup
) 访问此数据:json
re
import json
import re
import requests
from bs4 import BeautifulSoup
webpage = "https://www.zippia.com/amazon-com-careers-487/"
page = requests.get(webpage)
soup = BeautifulSoup(page.content, 'lxml')
for script in soup.find_all('script', {'type': 'text/javascript'}):
if 'getCompanyInfo' in script.text:
match = re.search("{[^\n]*}", script.text)
data = json.loads(match.group())
print(data["companyDiversity"]["languages"])
json.dump(data, open("test.json", "w"), indent=2) # Only if you want the data put in a readable format to a file (like if you want to find the path to an entry)
推荐阅读
- java - 在centos中使用java控制台创建目录但不使用Tomcat Web应用程序
- javascript - Node.js 所有 CSS 和 JS 链接在某个路径消失
- ios - 具有限制广告跟踪的 AdMob 激励视频/iOS
- python - 在 C++ 中对向量进行切片
- ios - CALayer 蒙版动画不消失
- javascript - 光标从不进入 $.ajax
- elasticsearch - Elasticsearch.Net 和 NEST 用于搜索字符串和标签数组
- c# - C# VFP OLEDB 查询抛出“命令包含无法识别的短语/关键字”
- linux - 编译错误 - Raspberry Pi 的内核源
- php - 如何将日期时间的日期部分转换为 PHP 中的时间戳?