使用嵌套框架和javascript进行网页抓取

时间:2014-01-15 04:25:36

标签: python beautifulsoup screen-scraping mechanize frames

我想从在线聊天机器人那里得到答案。 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>

我正在使用&#34;机械化&#34;和网页抓取的beautifulsoup,但我想机械化不支持动态网页。

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

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

2 个答案:

答案 0 :(得分:1)

无论是BeautifulSoup,机械化,请求甚至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

答案 1 :(得分:0)

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

import requests

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

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

由于您未提供有关动态网页的更多信息,因此没有更多可说的内容。

相关问题