python - Glassdoor 网页抓取与 Selenium

标签 python selenium web-scraping

我正在尝试抓取下面链接左下角图表中显示的评级趋势数据,但似乎无法找到获取它的方法。我担心这是因为它是作为图片嵌入的,因此数据无法访问,但我想我会检查一下。

添加了我缝合在一起的代码,但我只获得了轴值。

任何帮助将不胜感激。

https://www.glassdoor.com/Reviews/Netflix-Reviews-E11891.htm#trends-overallRating

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from time import sleep
import pandas as pd
from selenium.webdriver.common import action_chains, keys
from selenium.common.exceptions import NoSuchElementException
import numpy as np
import sys
import re
import warnings

options = Options()
options.headless = True


driver = webdriver.Chrome(r'PATH',options=options)
driver.get('https://www.glassdoor.com/Reviews/Netflix-Reviews-E11891.htm#trends-overallRating')

trend_element = driver.find_elements_by_xpath('//*[@id="DesktopTrendChart"]')[0]
trend = trend_element.text
print(trend)

最佳答案

我最初是使用 BeautifulSoup 尝试的。

我能够提取出相应值的所有坐标(我成功地做到了)。花了大约一个小时左右的时间找到它的全部位置,提取它,进入一个漂亮、整洁的数据框。

对于下一步,我打算将 x 和 y 坐标转换为相应的 x 和 y 标签,然后进行插值以创建更精细的数据集(我尚未尝试过)。我预计这还需要大约一个小时左右。

在这样做之前,我做了更多研究,发现了一篇有趣的文章 here .

读完之后,然后回到原来的问题,能够用 a) 更少的代码行来完成此操作,b) 没有 BeautifulSoup,c) 花了我大约 5-10 分钟才能完成,d)我学到了一些新东西。

因此,请阅读该链接,查看代码,这应该可以满足您的需求。

import requests
import json
import pandas as pd

url = 'https://www.glassdoor.co.uk/api/employer/11891-rating.htm?dataType=trend&category=overallRating&locationStr=&jobTitleStr=&filterCurrentEmployee=false'

with requests.Session() as se:
    se.headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36",
        "Accept-Encoding": "gzip, deflate",
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
        "Accept-Language": "en"
    }
    response = se.get(url)

data = json.loads(response.text)

results = pd.DataFrame()
results['date'], results['rating'] = data['dates'], data['employerRatings']

输出:

print (results)
          date  rating
0   2018/12/30  3.66104
1   2018/12/30  3.66311
2   2018/11/25  3.69785
3   2018/10/28  3.73478
4    2018/9/30  3.68311
5    2018/8/26  3.69093
6    2018/7/29  3.70312
7    2018/6/24  3.74851
8    2018/5/27  3.67543
9    2018/4/29  3.67500
10   2018/3/25  3.62248
11   2018/2/25  3.73467
12   2018/1/28  3.70791
13  2017/12/31  3.72217
14  2017/11/26  3.69733
15  2017/10/29  3.61443
16   2017/9/24  3.47046
17   2017/8/27  3.46511
18   2017/7/30  3.46711
19   2017/6/25  3.48164
20   2017/5/28  3.52925
21   2017/4/30  3.46825
22   2017/3/26  3.46874
23   2017/2/26  3.52620

关于python - Glassdoor 网页抓取与 Selenium ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54046254/

相关文章:

python - 类型错误 : write() takes at least 5 arguments (2 given) - inherited module from v8 to v10 community

java - selenium 浏览器什么时候自动死掉?

python - 从 craigslist 搜索中抓取每个图像

python - 不是 Python 3 和 Pandas 的 NaN 条件语句

python - 字节和字节数组的父类(super class)?

python - 仅允许整数步长时生成鼠标速度

java - Chrome 驱动程序无法在 Win XP 上加载

java - 如何为现有驱动程序对象设置 Firefox 配置文件

python - 使用 beautifulSoup 从 HTML 片段中的类获取 href 的所有值

python - 使用python登录Vk.com