当前位置: 首页 > 知识库问答 >
问题:

使用python刮取动态javascript内容网页

孙志
2023-03-14

我试图刮这个网站:https://ec.europa.eu/research/mariecurieactions/how-to/find-job_en使用Python。

首先,我注意到我感兴趣的表实际上位于以下url:https://ec.europa.eu/assets/eac/msca/jobs/import-jobs_en.htm

然而,请求BS4只给我超文本标记语言的页面源。我假设这是因为内容是动态的。

因此,我尝试了Selenium BS4来刮取网站,但我仍然只能刮取页面源代码。

from selenium.webdriver import Firefox
from bs4 import BeautifulSoup
import lxml

driver = Firefox()
url = 'https://ec.europa.eu/assets/eac/msca/jobs/import-jobs_en.htm'
driver.get(url)
soup = BeautifulSoup(driver.page_source, 'lxml')

我如何才能刮上述网站?

共有2个答案

宁锐
2023-03-14

实际上,您可以使用请求BS4获得所需的结果。您所需要做的就是使用APIhttps://euraxess.ec.europa.eu/sites/default/files/exports/msca.xml以及标头。

密码

import requests
from bs4 import BeautifulSoup

headers = {
    'authority': 'euraxess.ec.europa.eu',
    'pragma': 'no-cache',
    'cache-control': 'no-cache',
    'sec-ch-ua': '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
    'accept': 'application/xml, text/xml, */*; q=0.01',
    'sec-ch-ua-mobile': '?0',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.106 Safari/537.36',
    'origin': 'https://ec.europa.eu',
    'sec-fetch-site': 'same-site',
    'sec-fetch-mode': 'cors',
    'sec-fetch-dest': 'empty',
    'referer': 'https://ec.europa.eu/',
    'accept-language': 'en-US,en;q=0.9',
}

response = requests.get('https://euraxess.ec.europa.eu/sites/default/files/exports/msca.xml',headers=headers)
# print(response.text)

soup = BeautifulSoup(response.content, 'html.parser')
ID = soup.find_all('job-id')
Title = soup.find_all('job-title')
for ID,Title in zip(ID,Title):
    print(ID.text,Title.text)

输出

383876 PhD position in the framework of HEalth data LInkage for ClinicAL benefit (Helical) project
433411 PhD Student in Biophysics/Electrophysiology
454880 15 PhD positions in Marie Sklodowska Curie ITN “Active Monitoring of Cancer As An Alternative To Surgery” (CAST)
465392 15 Marie Curie PhD Positions in ''Mobility and Training for Beyond 5G Ecosystems (MOTOR5G)''
480654 Early Stage Research Position in mmWave-based communication systems at National Instruments Dresden GmbH
....
雷硕
2023-03-14

如果你更进一步,你会在这里找到真正的数据:https://euraxess.ec.europa.eu/sites/default/files/exports/msca.xml这里有一个使用SimplifiedDoc的例子。

from simplified_scrapy.request import req
from simplified_scrapy.simplified_doc import SimplifiedDoc
html = req.get('https://euraxess.ec.europa.eu/sites/default/files/exports/msca.xml') 
doc = SimplifiedDoc(html)
jobs = doc.selects('job-opportunity')
for job in jobs:
    print (job.select('job-id>text()'),job.select('job-title>text()'))

结果:

367020 Early-Stage Researcher (ESR) 3-year PhD position - "Efficient intra-cavity and extra-cavity generation of beams with radial and azimuthal polarization in high-power thin-disk lasers" - Project: GREAT
377512 8 Short-term Early Stage Researcher positions available through the EvoCELL ITN (single cell genomics, evo-devo and science outreach)
383978 ESR (early stage researcher) for intelligent quality control cycles in Industry 4.0 process chains enabled by machine learning
......
 类似资料:
  • 我正试图从这一页上删除所有5000家公司。当我向下滚动时,它的动态页面和公司被加载。但我只能刮去5家公司的钱,那我怎么能刮去全部5000家呢?当我向下滚动页面时,URL正在更改。我试过硒,但没用。https://www.inc.com/profile/onetrust注意:我想刮公司的所有信息,但刚才选择了两个。 更新了代码,但页面根本不滚动。更正了BeautifulSoup代码中的一些错误 谢谢

  • 问题内容: 我想使用Python在这样的网页上抓取“正在寻找这些作者:”框中的内容:http : //academic.research.microsoft.com/Search?query=lander 不幸的是,盒子的内容是由JavaScript动态加载的。通常在这种情况下,我可以阅读Javascript来了解发生了什么,或者可以使用Firebug之类的浏览器扩展来了解动态内容的来源。这次没有

  • 问题内容: 免责声明:我在StackOverflow上看到过许多其他类似的帖子,并尝试以相同的方式进行操作,但是它们似乎在此网站上不起作用。 我正在使用Python-Scrapy从koovs.com获取数据。 但是,我无法获得动态生成的产品尺寸。具体来说,如果有人可以引导我从此链接的下拉菜单中获取“不可用”尺寸标签,我将不胜感激。 我可以静态获取尺寸列表,但这样做只能得到尺寸列表,但不能获得其中的

  • 我想使用Python在这样的网页上抓取“你在寻找这些作者吗”框的内容:http://academic.research.microsoft.com/Search?query=lander 不幸的是,该框的内容是由JavaScript动态加载的。通常在这种情况下,我可以阅读Javascript来了解发生了什么,或者我可以使用Firebug之类的浏览器扩展来了解动态内容的来源。这次没有这样的运气。。。

  • 问题内容: 我正在使用Python 3.1,如果有帮助的话。 无论如何,我正在尝试获取此网页的内容。我用Google搜索了一下,尝试了不同的方法,但是它们没有用。我猜想这应该是一件容易的事,但是…我做不到。:/。 urllib,urllib2的结果: 谢谢杰森。:D。 问题答案: 由于您使用的是Python 3.1,因此需要使用新的Python 3.1 API 。 尝试: 或者,看起来您正在使用P

  • 本文向大家介绍python+selenium+PhantomJS抓取网页动态加载内容,包括了python+selenium+PhantomJS抓取网页动态加载内容的使用技巧和注意事项,需要的朋友参考一下 环境搭建 准备工具:pyton3.5,selenium,phantomjs 我的电脑里面已经装好了python3.5 安装Selenium pip3 install selenium 安装Phan