【问题标题】:BeautifulSoup not Correctly Taking all the HTMLBeautifulSoup 未正确获取所有 HTML
【发布时间】:2012-02-04 12:59:45
【问题描述】:

我正在尝试使用 Python 中的 BeautifulSoup 和 Mechanize 为一个学术项目编写一个简单的抓取程序。我试图从亚马逊获取一些产品的价格,因为我想测试他们定价模型的各种理论。我遇到的问题是 BeautifulSoup 随机不会从 Mechanize 获取整个 HTML 页面。我已将出现错误的时间打印到文本文件中,并且每次 Mechanize 页面完全形成时,但 BeautifulSoup 页面只有一半。这是我的代码:

def process_product_url(product_url):
    """Scrapes and returns all the data in the given product url"""
    #Download product_page given product_url
    product_page_mech, product_page_bs = get_product_page_mech_bs(product_url)

    #Extract Price
    price = extract_price(product_page_bs)
    return price

def get_product_page_mech_bs(url):
    """Takes a product page url in str and returns the mech page and bs page"""
    while True:
        mech_page = get_mech_page(url)
        bs_page = BeautifulSoup(unicode(mech_page.response().read(), 'latin-1'))
        if not test_product_page(bs_page):
            log(unicode(bs_page))
            log(unicode(mech_page.response().read(), 'latin-1'))
            continue
    return mech_page, bs_page

def test_product_page(product_page_bs):
    """Takes a BS product page and tests to see if proper"""
    if rank_page_bs.findAll('span', attrs={'id' : 'actualPriceValue'}) == []:
        return False
    else:
        return True

def get_mech_page(url):
    """Given a URL, returns Mechanize page object"""
    while True:
        try:
            br = initialize_browser()
            br.open(url)
            return br
        except Exception, e:
            print e
            print traceback.print_exc()
            continue

def initialize_browser():
    """Returns a fully setup mechanize browser instance"""
    br = mechanize.Browser()
    br.addheaders = [("User-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0.1) Gecko/20100101 Firefox/9.0.1")]
    return br

我已经上传了这个页面的BeautifulSoup outputMechanize output:http://www.amazon.com/Fujifilm-X-Pro-Digital-Camera-Body/dp/B006UV6YMQ/ref=sr_1_2?s= electronics&ie=UTF8&qid=1328359488&sr=1-2(我不能粘贴两个以上的链接)

编辑:澄清和扩展

【问题讨论】:

  • 也许如果我们有get_mech_page 的实现和一个展示问题的url,有人会尝试一下。
  • 或者,您能否打印mech_pagebs_page,并将它们与使用urllib2.urlopen(url).read() 返回的原始HTML 进行比较?

标签: python beautifulsoup mechanize


【解决方案1】:

我这样做了:

from BeautifulSoup import BeautifulSoup
import mechanize

def get_page_mech_bs(url):
    """Takes a page url and returns the mech page and bs page"""
    while True:
        mech_page = get_mech_page(url)
        bs_page = BeautifulSoup(unicode(mech_page.response().read(), 'latin-1'))
        if not test_page(bs_page):
            print "Error in page, redownloading"
            log(unicode(bs_page))
            log(unicode(mech_page.response().read(), 'latin-1'))
            continue
        else:
            break
    return mech_page, bs_page

def get_mech_page(url):
    br = mechanize.Browser()
    br.open(url)
    return br

def test_page(bs_page):
    return True

if __name__ == '__main__':
    print get_page_mech_bs("http://google.com")

我不知道test_page 是怎么写的。当test_page 为真时,我会退出循环。我从 BeautifulSoup 得到的 html 看起来是对的。有什么问题?

【讨论】:

    猜你喜欢
    • 2016-05-30
    • 2019-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 2019-05-14
    • 2013-07-13
    • 1970-01-01
    相关资源
    最近更新 更多