selenium 刮擦玻璃门概述

rryofs0p  于 2022-11-29  发布在  其他
关注(0)|答案(1)|浏览(134)

我想知道为什么我不能刮Glassdoor公司概况。一个例子是,我想刮沃尔玛的规模,这是10000+员工。下面是我的代码,不知道为什么我找的信息没有...

import requests
from bs4 import BeautifulSoup
import pandas as pd 

headers = {'user-agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.80 Safari/537.36'}
url = f'https://www.glassdoor.com/Overview/Working-at-Walmart-EI_IE715.11,18.htm'
    # f'https://www.glassdoor.com/Reviews/Google-Engineering-Reviews-EI_IE9079.0,6_DEPT1007_IP{pg}.htm?sort.sortType=RD&sort.ascending=false&filter.iso3Language=eng'

r = requests.get(url, headers)
soup = BeautifulSoup(r.content, 'html.parser')

我深深感谢任何帮助刮这个“大小”的因素在公司网页上。我真的不知道为什么这样一个简单的事情不能刮...请提供代码以及我是新的语言以外的bs4...谢谢你这么多。

rm5edbpk

rm5edbpk1#

以下是一个可能的解决方案:

import re
import json
import requests
from bs4 import BeautifulSoup

headers = {
    'user-agent': 'Mozilla/5.0'
}

with requests.Session() as session:
    session.headers.update(headers)
    raw_data = session.get(f'https://www.glassdoor.com/Overview/Working-at-Walmart-EI_IE715.htm').text
    
    script = [s.text for s in BeautifulSoup(raw_data, "lxml").find_all("script") if "window.appCache" in s.text][0]
    json_data = json.loads(re.findall(r'(\"Employer:\d+\":)(.+)(,\"ROOT_QUERY\")', script)[0][1])

    data = {
        "id": json_data["id"],
        "shortName": json_data["shortName"],
        "website": json_data["website"],
        "type": json_data["type"],
        "revenue": json_data["revenue"],
        "headquarters": json_data["headquarters"],
        "size": json_data["size"],
        "yearFounded": json_data["yearFounded"]
    }
    
    print(data)

输出量:

{
  'id': 715,
  'shortName': 'Walmart',
  'website': 'careers.walmart.com',
  'type': 'Company - Public',
  'revenue': '$10+ billion (USD)',
  'headquarters': 'Bentonville, AR',
  'size': '10000+ Employees',
  'yearFounded': 1962
}

如果您只需要“size”,则只需使用size = json_data["size"]

相关问题