使用 Requests 和 BeautifulSoup - Python 返回没有文本的标签 [英] Using Requests and BeautifulSoup - Python returns tag with no text

查看:33
本文介绍了使用 Requests 和 BeautifulSoup - Python 返回没有文本的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试捕获此页面上的访问次数,但 python 返回没有文本的标记.

I'm trying to capture the number of visits on this page, but python returns the tag with no text.

这就是我所做的.

import requests
from bs4 import BeautifulSoup

r = requests.get("http://www.kijiji.ca/v-2-bedroom-apartments-condos/city-of-halifax/clayton-park-west-condo-style-luxury-2-bed-den/1016364514")
soup = BeautifulSoup(r.content)
print soup.find_all("span",{"class":"ad-visits"})

推荐答案

您尝试抓取的值由 javascript 填充,因此 beautfulsouprequests 不会在这种情况下工作.

The values you are trying to scrape are populated by javascript so beautfulsoup or requests aren't going to work in this case.

您需要使用诸如 selenium 之类的东西来获取输出.

You'll need to use something like selenium to get the output.

from bs4 import BeautifulSoup
from selenium import webdriver

driver = webdriver.Firefox()
driver.get("http://www.kijiji.ca/v-2-bedroom-apartments-condos/city-of-halifax/clayton-park-west-condo-style-luxury-2-bed-den/1016364514")
soup = BeautifulSoup(driver.page_source , 'html.parser')
print soup.find_all("span",{"class":"ad-visits"})

Selenium 将返回呈现的页面源代码,然后您可以使用 beautifulsoup 获取值

Selenium will return the page source as rendered and you can then use beautifulsoup to get the value

[<span class="ad-visits">385</span>]

这篇关于使用 Requests 和 BeautifulSoup - Python 返回没有文本的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆