2014-01-15 28 views
0

我想从一个在线聊天机器人获得答案。 http://talkingbox.dyndns.org:49495/braintalk? (?在属于链接)使用嵌套帧和javascript的网页抓取

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

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> 

我所用“机械化”和美丽的网页刮,但我想机械化不支持动态网页。

如何在这种情况下得到答案?

我也在寻找一个在Windows和Linux上运行良好的解决方案。

+0

您可以尝试selenuim,它擅长浏览器自动化以及phantomjs(为无头Webkit提供JS API,Webkit是渲染引擎)的绑定。 http://www.realpython.com/blog/python/headless-selenium-testing-with-python-and-phantomjs/#.UtYORpDtn4w –

+0

什么是动态网页?这些框架只知道http请求,并且您分享的链接不可访问 –

+0

链接已死亡。 –

回答

0

我会使用Requests这样的任务。

import requests 

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

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

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

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