python-3.x - 在python 3中使用requests.get获取数据之前等待页面加载

标签 python-3.x web-scraping beautifulsoup python-requests

我有一个页面,我需要获取与 BS4 一起使用的源,但是页面中间需要 1 秒(可能更少)来加载内容,并且 requests.get 在该部分加载之前捕获页面的源,如何在获取数据之前我可以等一下吗?

r = requests.get(URL + self.search, headers=USER_AGENT, timeout=5 )
    soup = BeautifulSoup(r.content, 'html.parser')
    a = soup.find_all('section', 'wrapper')

The page
<section class="wrapper" id="resultado_busca">

最佳答案

看起来不是等待的问题,看起来元素是由JavaScript创建的,requests无法处理 JavaScript 动态生成的元素。一个建议是使用 selenium 连同 PhantomJS 获取页面源,然后可以使用BeautifulSoup对于您的解析,下面显示的代码将完全做到这一点:

from bs4 import BeautifulSoup
from selenium import webdriver

url = "http://legendas.tv/busca/walking%20dead%20s03e02"
browser = webdriver.PhantomJS()
browser.get(url)
html = browser.page_source
soup = BeautifulSoup(html, 'lxml')
a = soup.find('section', 'wrapper')

此外,没有必要使用 .findAll如果您只寻找一种元素。

关于python-3.x - 在python 3中使用requests.get获取数据之前等待页面加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45448994/

相关文章:

python - 检测 → Python 中的符号

python - 为什么这在 Python IDLE shell 中有效,但当我从命令提示符将其作为 Python 脚本运行时却无效?

python - 在抓取中查找网络图像的位置

python - 比较两个文本文件并仅将差异附加到一个文本文件中

python - BS4,在未闭合的 <br> 之间进行精确匹配

python - pandas - 如何将嵌套字典中的数据加载到数据框中?

python-3.x - mpld3 未在 Colab 中显示图形

asp.net - 如何防止我的 ASP.NET 网站被屏幕抓取?

python - 使用 Python 时无法在 chrome 开发工具中提取正确的元素

python - 在 BeautifulSoup 中查找标签和文本