Python 使用请求的某些站点上的HTML响应不完整&;硒素

Python 使用请求的某些站点上的HTML响应不完整&;硒素,python,selenium,beautifulsoup,python-requests,Python,Selenium,Beautifulsoup,Python Requests,我打算使用Python中的请求和美化组从一些URL中获取信息。但有些网站只返回部分HTML响应,而没有返回页面内容 这是不起作用的代码: import requests from bs4 import BeautifulSoup url = "http://www.exampleurl.com" r = requests.get(url) soup = BeautifulSoup(r.content, 'html.parser') 以下是不完整的回答: 我尝试将Selen

我打算使用Python中的请求和美化组从一些URL中获取信息。但有些网站只返回部分HTML响应,而没有返回页面内容

这是不起作用的代码:

import requests
from bs4 import BeautifulSoup
url = "http://www.exampleurl.com"
r = requests.get(url)
soup = BeautifulSoup(r.content, 'html.parser')
以下是不完整的回答:

我尝试将Selenium与Chrome Webdriver结合使用,但最终还是遇到了同样的问题

from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--incognito')
options.add_argument('--headless')
browser = webdriver.Chrome(options=options)
browser.get(url)
html = browser.page_source

有什么想法吗?

发生了什么事

  • 您无法获得预期的html,因为它位于iframe中
  • 尝试获取iframe
    soup的src。查找('iframe')['src']
    并再次请求
  • 示例

    import requests
    from bs4 import BeautifulSoup
    url = "http://www.ingenieur-jobs.de/jobangebote/3075/"
    r = requests.get(url)
    soup = BeautifulSoup(r.content, 'html.parser')
    
    iframe = requests.get(soup.find('iframe')['src'])
    
    soup = BeautifulSoup(iframe.content, 'html.parser')
    soup
    
    请看这个