Python 删除站点丢失的数据
因此,我试图在这个网站上搜索打开的位置,当我使用任何类型的请求(目前正在尝试请求html)时,它不会显示html中的所有内容Python 删除站点丢失的数据,python,web-scraping,python-requests,Python,Web Scraping,Python Requests,因此,我试图在这个网站上搜索打开的位置,当我使用任何类型的请求(目前正在尝试请求html)时,它不会显示html中的所有内容 # Import libraries import time from bs4 import BeautifulSoup from requests_html import HTMLSession # Set the URL you want to webscrape from url = 'https://germanamerican.csod.com/ux/ats/
# Import libraries
import time
from bs4 import BeautifulSoup
from requests_html import HTMLSession
# Set the URL you want to webscrape from
url = 'https://germanamerican.csod.com/ux/ats/careersite/5/home?c=germanamerican'
session = HTMLSession()
# Connect to the URL
response = session.get(url)
response.html.render()
# Parse HTML and save to BeautifulSoup object¶
soup = BeautifulSoup(response.text, "html5lib")
b = soup.findAll('a')
不知道去哪里。最初认为问题是由于javascript渲染造成的,但这不起作用。欢迎使用
不幸的是,您将无法使用请求
(或请求_html
或类似库)刮取该页面,因为您需要一个工具来处理动态页面,即基于javascript的页面
对于python,我强烈建议使用selenium
及其webdriver
。下面是一段打印所需输出的代码,即列出的所有作业(注意,需要安装selenium
和Firefox webdriver,并使用正确的运行路径)
我认为不可能用请求刮去那个网站。
我建议使用Selenium或Scrapy。问题是初始GET没有获得数据(我假设是工作列表),而执行此操作的js使用了一个在标题中带有授权令牌的POST。您需要获取此令牌,然后进行POST以获取数据 这个标记看起来是动态的,所以我们在获取它时会有点不确定,但它是可行的
url0=r'https://germanamerican.csod.com/ux/ats/careersite/5/home?c=germanamerican'
url=r'https://germanamerican.csod.com/services/x/career-site/v1/search'
s=HTMLSession()
r=s.get(url0)
print(r.status_code)
r.html.render()
soup=bs(r.text,'html.parser')
scripts=soup.find_all('script')
for script in scripts:
if 'csod.context=' in script.text: x=script
j=json.loads(x.text.replace('csod.context=','').replace(';',''))
payload={
'careerSiteId': 5,
'cities': [],
'countryCodes': [],
'cultureId': 1,
'cultureName': "en-US",
'customFieldCheckboxKeys': [],
'customFieldDropdowns': [],
'customFieldRadios': [],
'pageNumber': 1,
'pageSize': 25,
'placeID': "",
'postingsWithinDays': None,
'radius': None,
'searchText': "",
'states': []
}
headers={
'accept': 'application/json; q=1.0, text/*; q=0.8, */*; q=0.1',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.9',
'authorization': 'Bearer '+j['token'],
'cache-control': 'no-cache',
'content-length': '272',
'content-type': 'application/json',
'csod-accept-language': 'en-US',
'origin': 'https://germanamerican.csod.com',
'referer': 'https://germanamerican.csod.com/ux/ats/careersite/5/home?c=germanamerican',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36',
'x-requested-with': 'XMLHttpRequest'
}
r=s.post(url,headers=headers,json=payload)
print(r.status_code)
print(r.json())
打印出来的
r.json()
是一个很好的json格式的工作列表表 您对“当前空缺”感兴趣吗?响应的状态代码是什么?他正在使用请求-htmlrequests_html处理动态页面您从何处获得有效负载和标题?我基本上理解你的意思。@SamuelKhong检查了我的浏览器在我访问网站时发出的请求。嗨,我还有一个问题要问。所以我想在链接中刮取一个链接。e、 我对html请求非常陌生,所以有点挣扎-x@SamuelKhong你在寻找链接的哪一部分?
url0=r'https://germanamerican.csod.com/ux/ats/careersite/5/home?c=germanamerican'
url=r'https://germanamerican.csod.com/services/x/career-site/v1/search'
s=HTMLSession()
r=s.get(url0)
print(r.status_code)
r.html.render()
soup=bs(r.text,'html.parser')
scripts=soup.find_all('script')
for script in scripts:
if 'csod.context=' in script.text: x=script
j=json.loads(x.text.replace('csod.context=','').replace(';',''))
payload={
'careerSiteId': 5,
'cities': [],
'countryCodes': [],
'cultureId': 1,
'cultureName': "en-US",
'customFieldCheckboxKeys': [],
'customFieldDropdowns': [],
'customFieldRadios': [],
'pageNumber': 1,
'pageSize': 25,
'placeID': "",
'postingsWithinDays': None,
'radius': None,
'searchText': "",
'states': []
}
headers={
'accept': 'application/json; q=1.0, text/*; q=0.8, */*; q=0.1',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.9',
'authorization': 'Bearer '+j['token'],
'cache-control': 'no-cache',
'content-length': '272',
'content-type': 'application/json',
'csod-accept-language': 'en-US',
'origin': 'https://germanamerican.csod.com',
'referer': 'https://germanamerican.csod.com/ux/ats/careersite/5/home?c=germanamerican',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36',
'x-requested-with': 'XMLHttpRequest'
}
r=s.post(url,headers=headers,json=payload)
print(r.status_code)
print(r.json())