句子。拆分获取网站页面

2024-04-19 03:34:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个程序,从tv Cathup网站获取html,然后使用split函数将所有html数据拆分为频道名称和表中当前正在播放的节目,例如:BBC 1-'program name'。我只是需要帮助我做什么后,我的第一次分裂功能,如果有人可以帮助,将不胜感激。你知道吗

更新-因为这是一个学校的项目,我需要使用句子。拆分功能,我很困惑,我会做什么,从这一点开始,如果有人可以指出我的正确方向,下一阶段的网站,我需要分裂等?你知道吗

import urllib2
import string


proxy = urllib2.ProxyHandler({"http" : "http://c99.cache.e2bn.org:8084"})

opener = urllib2.build_opener(proxy)

urllib2.install_opener(opener)

tvCatchup = urllib2.urlopen('http://www.TVcatchup.com')

html = tvCatchup.read()

firstSplit = html.split('<a class="enabled" href="/watch.html?c=')[1:]
for i in firstSplit:
    print i

secondSplit = html.split ('1" title="BBC One"></a></li><li class="v-type" style="color:#6d6d6d;">')[1:]

for i in secondSplit:
    print i

Tags: inimport功能httpfor网站htmlopener
1条回答
网友
1楼 · 发布于 2024-04-19 03:34:58

您通常会使用html parser(参见Python HTMLParser的示例)来实现这一点。(人们也经常使用^{})。使用split是可能的,但是有点不太成熟。。。我还是做了。在最初将页面分割成大段之后,下一步是循环浏览这些页面,并将它们分割成更小的段,磨练您想要的信息。你知道吗

big_parts = html.split('href="/watch.html?c=')[1:]
for n, part in enumerate(big_parts):
    small_part = part.split('</a>')[0]
    if n % 2:       # odd numbered segments
        programme = small_part.split('"> ')[1]
        print programme
    else:           # even numbered segments
        smaller_parts = small_part.split('"')
        number = smaller_parts[0]
        channel = smaller_parts[2]
        print number, channel, ':', 

它之所以有效,是因为在href="/watch.html?c=</a>之间查找文本时,恰好标识了同时包含频道名和节目名的所有段。然后可以使用识别字符序列(">")来分解这些段,以获得所需的确切信息。如果网站改变了它的HTML样式,这可能会停止工作。你知道吗

相关问题 更多 >