Python打开链接进入链接

标签 python urllib2

您好,我想重用输出链接以打开到输出网站的新链接。通过 RSS Feed,我实现了链接。我需要处理所有输出链接。使用哪个代码可以实现这一点?

import urllib2
import re

htmlfile = urllib2.urlopen('http://www.spiegel.de/schlagzeilen/tops/index.rss')
htmltext = htmlfile.read()
regex = '<guid>(.+?)</guid>'
pattern = re.compile(regex)
links = re.findall(pattern,htmltext)
downloadlinks = ''
for i, link in enumerate(links):
    if i == 0:
        downloadlinks += link
    else:
        downloadlinks += ' ' + link

print (downloadlinks)

输出是:

http://www.spiegel.de/panorama/leute/jennifer-lopez-singt-beim-geburtstag-von-turkmenistans-praesident-a-908601.html
http://www.spiegel.de/sport/sonst/tony-martin-setzt-tour-de-france-trotz-sturz-fort-a-908600.html
http://www.spiegel.de/politik/ausland/ecuador-schiebt-verantwortung-fuer-snowden-auf-russland-a-908595.html
http://www.spiegel.de/panorama/wetter-temperaturrekorde-im-westen-der-usa-a-908593.html http://www.spiegel.de/politik/deutschland/polizei-raeumt-camp-hungerstreikender-fluechtinge-in-muenchen-a-908592.html
...

另一个例子:

import urllib2
import re

htmlfile = urllib2.urlopen('http://www.kino.de/rss/neu-im-kino/')
htmltext = htmlfile.read()
regex = '<link>(.+?)</link>'
pattern = re.compile(regex)
links = re.findall(pattern,htmltext)
downloadlinks = ''
for i, link in enumerate(links):
    if i == 0:
        downloadlinks += link
    else:
        downloadlinks += ' ' + link

print (downloadlinks)

'--------------------------------------------------------------------------------------    --------------------------'

htmlfile_2 = urllib2.urlopen(downloadlinks)
htmltext_2 = htmlfile_2.read()
regex_2 = '<meta itemprop="contentURL" content="(.+?)" />'
pattern_2 = re.compile(regex_2)
links_2 = re.findall(pattern_2,htmltext_2)
downloadlinks_2 = ''
for i, link in enumerate(links_2):
    if i == 0:
        downloadlinks_2 += link
    else:
        downloadlinks_2 += ' ' + link

print (downloadlinks_2)

输出是:

http://www.kino.de/kinofilm/the-deep/130585
http://www.kino.de/kinofilm/englisch-fuer-anfaenger/145880
http://www.kino.de/kinofilm/the-grandmaster/147546 
http://www.kino.de/kinofilm/jets-helden-der-luefte/148993
http://www.kino.de/kinofilm/laurence-anyways/144027
http://www.kino.de/kinofilm/modest-reception-die-macht-des-geldes/142819
http://www.kino.de/kinofilm/papadopoulos-und-soehne/145922
http://www.kino.de/kinofilm/seitengaenge/132599
http://www.kino.de/kinofilm/a-silent-rockumentary/149048
http://www.kino.de/kinofilm/world-war-z/120130

我想要这个:

htmlfile_2 = urllib2.urlopen(http://www.kino.de/kinofilm/the-deep/130585)

输出是:

http://flashvideo.kino.de/video/clipfile/627/000551627.mp4

最佳答案

简单地迭代每个原始链接,打印出所有子链接。

import urllib2
import re

htmlfile = urllib2.urlopen('http://www.kino.de/rss/neu-im-kino/')
htmltext = htmlfile.read()
regex = '<link>(.+?)</link>'
pattern = re.compile(regex)
links = re.findall(pattern,htmltext)

print( ' '.join(links) ) # or print( '\n'.join(links) )


for link in links:
    htmlfile_2 = urllib2.urlopen(link)
    htmltext_2 = htmlfile_2.read()
    regex_2 = '<meta itemprop="contentURL" content="(.+?)" />'
    pattern_2 = re.compile(regex_2)
    links_2 = re.findall(pattern_2,htmltext_2)

    print( ' '.join(links_2) ) # or print( '\n'.join(links_2) )

关于Python打开链接进入链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17389302/

相关文章:

python - Numpy 设置错误 : extra formal argument

javascript - 根据需要,从 jquery 事件有选择地关闭各个子 python 进程

python - 使用VerbNet、NLTK解析动词

python - 使用 urllib2 将 solr curl updateJSON 语法转换为 python

python - 如何在 Python 中跟踪元刷新

python - 如何实现tensorflow session 配置

python - 有效合并 Pandas 中的多个数据框

python - urllib没有返回请求的内容

Python 得到错误的 UTF-8 字符编码?

python - 如何查看下载过程?