Python 3.x Python-使用HTML标记的Web抓取
我正试图在网页上列出URL中发布的工作: 有关网页检查的详细信息,请参阅图片 通过网页检查观察到以下情况:Python 3.x Python-使用HTML标记的Web抓取,python-3.x,web-scraping,beautifulsoup,urllib2,Python 3.x,Web Scraping,Beautifulsoup,Urllib2,我正试图在网页上列出URL中发布的工作: 有关网页检查的详细信息,请参阅图片 通过网页检查观察到以下情况: 列出的每个作业都位于带有class=“jobs list item”的HTML li中。Li在Li中的父Div中包含以下html标记和数据 职位text=“软件工程师II”的数据ph值, 工作类别text=“工程”的数据ph值, 工作岗位日期的数据ph text=“2018-03-19T16:33:00” 带有class=“information”的父Div中的第一个子Div具有带有url
如果你想通过请求来实现这一点,你需要对网站进行反向工程。在Chrome中打开开发工具,选择网络选项卡并填写表单 这将显示站点如何加载数据。如果您深入该站点,您将看到它通过向该端点发送帖子来获取数据:。它还向您显示站点使用的有效负载。该站点使用cookie,所以您所要做的就是创建一个会话来保存cookie,获取cookie并复制/粘贴负载 通过这种方式,您将能够提取javascript获取的用于动态填充站点的相同json数据 下面是一个工作的例子,它看起来像什么。剩下的就是按照您认为合适的方式解析json
import requests
from pprint import pprint
# create a session to grab a cookie from the site
session = requests.Session()
r = session.get("https://careers.microsoft.com/us/en/")
# these params are the ones that the dev tools show that site sets when using the website form
payload = {
"lang":"en_us",
"deviceType":"desktop",
"country":"us",
"ddoKey":"refineSearch",
"sortBy":"",
"subsearch":"",
"from":0,
"jobs":"true",
"counts":"true",
"all_fields":["country","state","city","category","employmentType","requisitionRoleType","educationLevel"],
"pageName":"search-results",
"size":20,
"keywords":"",
"global":"true",
"selected_fields":{"city":["Hyderabad"],"country":["India"]},
"sort":"null",
"locationData":{}
}
# this is the endpoint the site uses to fetch json
url = "https://careers.microsoft.com/widgets"
r = session.post(url, json=payload)
data = r.json()
job_list = data['refineSearch']['data']['jobs']
# the job_list will hold 20 jobs (you can se the parameter in the payload to a higher number if you please - I tested 100, that returned 100 jobs
job = job_list[0]
pprint(job)
干杯。您需要使用任何浏览器模拟器,如
selenium
从该页面提取所需数据,因为这些数据是动态生成的。感谢您的建议。我对使用Python的Selenium一无所知。你能给我指一些我可以调整的工作方案样本吗?谢谢。您的解决方案非常有效。我对你对想使用“请求”的评论感到好奇。如果可以的话,我想使用一个更简单的选项。对于我有限的chrome开发者工具知识来说,反向工程网站似乎是一个复杂的方面。有没有更简单的解决方案?我知道@SIM建议使用硒元素。不客气。您询问了如何处理请求。你就是这样做的。我会使用请求来完成这样的工作。如果您想探索如何使用例如selenium实现这一点,请关闭此问题,并询问一个关于该主题的新问题。但是,请记住,使用这样的解决方案会慢得多,所以如果你真的要刮的话,这不是一个好办法……很高兴知道。“请求”是前进的方向。谢谢你的小费。我会记住的。我也接受了你的回答
import requests
from pprint import pprint
# create a session to grab a cookie from the site
session = requests.Session()
r = session.get("https://careers.microsoft.com/us/en/")
# these params are the ones that the dev tools show that site sets when using the website form
payload = {
"lang":"en_us",
"deviceType":"desktop",
"country":"us",
"ddoKey":"refineSearch",
"sortBy":"",
"subsearch":"",
"from":0,
"jobs":"true",
"counts":"true",
"all_fields":["country","state","city","category","employmentType","requisitionRoleType","educationLevel"],
"pageName":"search-results",
"size":20,
"keywords":"",
"global":"true",
"selected_fields":{"city":["Hyderabad"],"country":["India"]},
"sort":"null",
"locationData":{}
}
# this is the endpoint the site uses to fetch json
url = "https://careers.microsoft.com/widgets"
r = session.post(url, json=payload)
data = r.json()
job_list = data['refineSearch']['data']['jobs']
# the job_list will hold 20 jobs (you can se the parameter in the payload to a higher number if you please - I tested 100, that returned 100 jobs
job = job_list[0]
pprint(job)