为什么请求和urllib2丢失了网页上的一些文字?

时间:2016-01-19 18:40:46

标签: python html web-scraping python-requests urllib2

以下代码提取网页信息

from BeautifulSoup import BeautifulSoup
import requests
import urllib2

url = 'http://www.surfline.com/surf-report/rincon-southern-california_4197/'

source_code = requests.get(url)
plain_text = source_code.text
print plain_text

site = urllib2.urlopen(url).read()
print site

两个库结果包括:

<div id="current-surf-range" style="font-size:21px;font-weight:bold;padding-top:7px; padding-bottom: 7px;"></div>

不幸的是,这与实际网页不同:

<div id="current-surf-range" style="font-size:21px;font-weight:bold;padding-top:7px; padding-bottom: 7px;">4-5ft</div>

4-5英尺不存在,因此无法通过BeautifulSoup提取。

1 个答案:

答案 0 :(得分:1)

  1. docs
  2. 中安装selenium完整说明
      

    pip3安装selenium

    1. 下载驱动程序。我更喜欢使用chrome driver,但是如果安装了firefox,下面的代码应该可以正常工作。
    2. from selenium import webdriver
      url = 'http://www.surfline.com/surf-report/rincon-southern-california_4197/'
      web = webdriver.Firefox()
      # web = webdriver.Remote('http://localhost:9515', desired_capabilities=DesiredCapabilities.CHROME)
      
      source_code = web.get(url)
      # Sometimes it take time to load the page that's why: from time import sleep; sleep(2)
      plain_text = source_code.page_source