【问题标题】:Web scraping with nested frames and javascript使用嵌套框架和 javascript 进行网页抓取
【发布时间】:2014-01-15 04:25:36
【问题描述】:

我想从在线聊天机器人获得答案。 http://talkingbox.dyndns.org:49495/braintalk? (? 属于链接)

要发送问题,您只需发送一个简单的请求:

http://talkingbox.dyndns.org:49495/in?id=3B9054BC032E53EF691A9A1803040F1C&msg=[Here the question]

来源如下:

<frameset cols="*,185" frameborder="no" border="0" framespacing="0">
<frameset rows="100,*,82" frameborder="no" border="0" framespacing="0">
    <frame src="http://thebot.de/bt_banner.html" marginwidth="0" name="frtop" scrolling="no" marginheight="0" frameborder="no">
    <frame src="out?id=3B9054BC032E53EF691A9A1803040F1C" name="frout" marginwidth="0" marginheight="0">
    <frameset rows="100%,*" border="0" framespacing="0" frameborder="no">
        <frame src="bt_in?id=3B9054BC032E53EF691A9A1803040F1C" name="frin" scrolling="no" marginwidth="0" marginheight="0" noresize>
        <frame src="" name="frempty" marginwidth="0" marginheight="0" scrolling="auto" frameborder="no" >
    </frameset>
</frameset>
<frameset frameborder="no" border="0" framespacing="0" rows="82,*">
    <frame src="stats?" name="fr1" scrolling="no" marginwidth="0" marginheight="0" frameborder="no">
    <frame src="http://thebot.de/bt_rechts.html" name="fr2" scrolling="auto" marginwidth="0" marginheight="0" frameborder="no" >
</frameset>
</frameset>

我使用“mechanize”和 beautifulsoup 进行网页抓取,但我认为 mechanize 不支持动态网页。

在这种情况下我怎样才能得到答案?

我也在寻找一种适用于 Windows 和 Linux 的解决方案。

【问题讨论】:

  • 你可以试试selenuim,它擅长浏览器自动化以及它对phantomjs的绑定(它为headless Webkit提供了JS API,Webkit是一个渲染引擎)。 realpython.com/blog/python/…
  • 什么是动态网页?这些框架只知道http请求,而且你分享的链接是不可访问的
  • @Guy 我认为“user317”是指通过 XHR 请求获取的内容,但我同意更多细节会有所帮助。
  • 对不起,链接是talkingbox.dyndns.org:49495/braintalk? (最后的 ? 被stackoverflow切断)。框架的 Url 是 talkingbox.dyndns.org:49495/… 问题是无法像这样访问框架。
  • 感谢您的帮助。我可能必须澄清我在获得答案而不发送它们时遇到问题,因为嵌套框架内的答案是使用 JavaScript 动态生成的。本帧:talkingbox.dyndns.org:49495/…

标签: python beautifulsoup screen-scraping mechanize frames


【解决方案1】:

无论是 BeautifulSoup、mechanize、Requests 还是 Scrapy,加载动态页面都必须由您编写的另一个步骤来完成。

例如,使用 scrapy 可能看起来像:

class TheBotSpider(BaseSpider):
    name = 'thebot'
    allowed_domains = ['thebot.de', 'talkingbox.dyndns.org']

    def __init__(self, *a, **kw):
        super(TheBotSpider, self).__init__(*a, **kw)
        self.domain = 'http://talkingbox.dyndns.org:49495/'
        self.start_urls = [self.domain + 
                           'in?id=3B9054BC032E53EF691A9A1803040F1C&msg=' + 
                           self.question]

    def parse(self, response):
        sel = Selector(response)
        url = sel.xpath('//frame[@name="frout"]/@src').extract()[0]
        yield Request(url=url, callback=dynamic_page)

    def dynamic_page(self, response):
        .... xpath to scrape answer

以问题作为参数运行它:

scrapy crawl thebot -a question=[Here the question]

有关如何使用scrapy的更多详细信息,请参阅scrapy tutorial

【讨论】:

    【解决方案2】:

    我会使用Requests 来完成这样的任务。

    import requests
    
    r = requests.get("http://talkingbox.dyndns.org:49495/in?id=3B9054BC032E53EF691A9A1803040F1C&msg=" + your_question)
    

    对于不包含动态内容的网页,r.text 是你想要的。

    由于您没有提供更多关于动态网页的信息,因此无需多说。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-27
      • 1970-01-01
      • 2015-09-06
      • 2019-02-12
      • 2021-09-10
      相关资源
      最近更新 更多