python - 在某些使用 Requests & BeautifulSoup 或 Selenium 的网站上不完整的 HTML 响应
问题描述
我想在 Python 中使用 Requests 和 BeautifulSoup 从一些 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 的 src
soup.find('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
推荐阅读
- javascript - 进行基于角色的访问控制的正确方法
- linux - /proc/$pid/maps 文件中没有显示的许多地址呢?
- python - 跳过 matplotlib 错误栏的第一个错误
- kubernetes - 如何多次运行 AutoScalling?
- android - 是否可以获取 TextView 的文本 Rects?
- git - 在 git rebase 上将“pick”更改为“squash”的快捷方式
- mql4 - 为什么 MQL4 回测 RSI 值与图表上的 RSI 不匹配?
- python - Pandas 枢轴热图过滤最常见的值
- javascript - 带有背景图像的梯形 div,在滚动时会向矩形移动
- angular - 如何从 Angular 9 中的 Dom 中删除一个元素