python requests 动态加载_等待页面加载,然后在python 3中获取带有requests.get的数据...

I have a page that i need to get the source to use with BS4, but the middle of the page takes 1 second(maybe less) to load the content, and requests.get catches the source of the page before the section loads, how can I wait a second before getting the data?

r = requests.get(URL + self.search, headers=USER_AGENT, timeout=5 )

soup = BeautifulSoup(r.content, 'html.parser')

a = soup.find_all('section', 'wrapper')

解决方案

It doesn't look like a problem of waiting, it looks like the element is being created by JavaScript, requests can't handle dynamically generated elements by JavaScript. A suggestion is to use

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')

Also, there's no need to use .findAll if you are only looking for one element only.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值