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

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

问题描述

我试图捕捉到了这个页面上访问次数,但蟒蛇返回没有文字标签。

这是我做了什么。

 进口要求
从BS4进口BeautifulSoupR = requests.get(\"http://www.kijiji.ca/v-2-bedroom-apartments-condos/city-of-halifax/clayton-park-west-condo-style-luxury-2-bed-den/1016364514\")
汤= BeautifulSoup(r.content)
打印soup.find_all(跨越,{级:广告访问})


解决方案

您正试图刮掉由JavaScript的填充值,所以 beautfulsoup 请求不会在这种情况下工作。

您将需要使用像来得到输出。

 从BS4进口BeautifulSoup
硒进口的webdriver司机= 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\")
汤= BeautifulSoup(driver.page_source,'html.parser')
打印soup.find_all(跨越,{级:广告访问})

将返回页面源作为渲染,然后你可以使用 beautifulsoup 来获取值

  [<跨度类=广告访问> 385℃; / SPAN>]

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

This is what I've done.

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"})

解决方案

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

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 will return the page source as rendered and you can then use beautifulsoup to get the value

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

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

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