Python在我的25个链接中只放了一个字符串

2024-05-20 02:03:37 发布

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

我的问题是,我的脚本只将我从rss提要中读取的25个链接中的一个转换为我想要的评论数。我要做什么样的改变才能把所有的链接转换成评论的数量而不是只有一条?你知道吗

Python代码:

import re
import urllib
import urllib2

htmlfile = urllib2.urlopen('http://www.computerbase.de/rss/news.xml')
htmltext = htmlfile.read()
regex = "<link>(.+?)</link>"
pattern = re.compile(regex)
links = re.findall(pattern,htmltext)

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

for link in links:
    htmlfile_2 = urllib2.urlopen(link)
    htmltext_2 = htmlfile_2.read()
    htmltext_3 = htmltext_2.replace("/forum/", "http://www.computerbase.de/forum/")
    regex_2 = '<a href="(.+?)" id="comments-link"'
    pattern_2 = re.compile(regex_2)
    links_2 = re.findall(pattern_2,htmltext_3)
    print( ' '.join(links_2) )

for link_neu in links_2:
    htmlfile_neu = urllib2.urlopen(link_neu)
    htmltext_neu = htmlfile_neu.read()
    regex_neu = 'class="postcounter">#(.+?)<'
    pattern_neu = re.compile(regex_neu)
    links_neu = re.findall(pattern_neu,htmltext_neu)
    print( ' '.join(links_neu) )

输出:

http://www.computerbase.de/
http://www.computerbase.de/
http://www.computerbase.de/news/2013-07/simcity-koennte-offline-modus-erhalten/
http://www.computerbase.de/news/2013-07/das-motorola-moto-x-wird-individualisierbar-sein/
http://www.computerbase.de/news/2013-07/goty-version-von-borderlands-2-kuendigt-sich-an/
http://www.computerbase.de/news/2013-07/monitor-laesst-nutzer-3d-objekte-erfuehlen/
http://www.computerbase.de/news/2013-07/quadratisch-und-flexibel-be-quiet-shadow-rock-2/
http://www.computerbase.de/news/2013-07/schwere-android-sicherheitsluecke-betrifft-alle-geraete/
http://www.computerbase.de/artikel/handys/2013/nokia-lumia-925-im-test/
http://www.computerbase.de/news/2013-07/amds-radeon-hd-7730-steht-kurz-vor-marktstart/
http://www.computerbase.de/news/2013-07/apple-stellt-ceo-der-marke-yves-saint-laurent-ein/
http://www.computerbase.de/news/2013-07/zwoelf-22-nm-atom-celeron-und-pentium-benannt/
http://www.computerbase.de/news/2013-07/das-nokia-lumia-1020-mit-41-mp-kamera-zeigt-sich/
http://www.computerbase.de/news/2013-07/nikon-kuendigt-450-mm-wafer-scanner-fuer-2015-an/
http://www.computerbase.de/news/2013-07/prism-regierung-gibt-sich-immer-noch-ahnungslos/
http://www.computerbase.de/news/2013-07/cooler-master-bringt-schlichte-mechanische-tastatur/
http://www.computerbase.de/news/2013-07/spieleklassiker-outcast-soll-wiederbelebt-werden/
http://www.computerbase.de/news/2013-07/opera-15-macht-vieles-anders/
http://www.computerbase.de/news/2013-07/htc-schliesst-android-4.2-fuer-one-s-aus/
http://www.computerbase.de/news/2013-07/erneut-faelschungen-von-office-2010-im-umlauf/
http://www.computerbase.de/news/2013-07/libreoffice-will-mit-amd-die-gpu-unterstuetzung-verbessern/
http://www.computerbase.de/news/2013-07/tablets-mit-firefox-os-in-vorbereitung/
http://www.computerbase.de/news/2013-07/nokia-bringt-feature-phones-mit-umts-fuer-70-dollar/
http://www.computerbase.de/news/2013-07/erstes-benchmarkergebnis-von-intels-bay-trail-t-aufgetaucht/
http://www.computerbase.de/news/2013-07/broken-age-wird-in-zwei-teilen-veroeffentlicht/
http://www.computerbase.de/news/2013-07/amd-plant-sparsame-t-modelle-von-richland/
http://www.computerbase.de/news/2013-07/fm2-mainboard-von-biostar-fuer-audiophile-mit-amd-apus/


http://www.computerbase.de/forum/showthread.php?t=1227204
http://www.computerbase.de/forum/showthread.php?t=1226943
http://www.computerbase.de/forum/showthread.php?t=1227135
http://www.computerbase.de/forum/showthread.php?t=1227123
http://www.computerbase.de/forum/showthread.php?t=1227120
http://www.computerbase.de/forum/showthread.php?t=1227113
http://www.computerbase.de/forum/showthread.php?t=1227077
http://www.computerbase.de/forum/showthread.php?t=1227036
http://www.computerbase.de/forum/showthread.php?t=1227062
http://www.computerbase.de/forum/showthread.php?t=1227058
http://www.computerbase.de/forum/showthread.php?t=1227047
http://www.computerbase.de/forum/showthread.php?t=1227037
http://www.computerbase.de/forum/showthread.php?t=1227016
http://www.computerbase.de/forum/showthread.php?t=1226986
http://www.computerbase.de/forum/showthread.php?t=1226977
http://www.computerbase.de/forum/showthread.php?t=1226962
http://www.computerbase.de/forum/showthread.php?t=1226946
http://www.computerbase.de/forum/showthread.php?t=1226913
http://www.computerbase.de/forum/showthread.php?t=1226872
http://www.computerbase.de/forum/showthread.php?t=1226820
http://www.computerbase.de/forum/showthread.php?t=1226818
http://www.computerbase.de/forum/showthread.php?t=1226753
http://www.computerbase.de/forum/showthread.php?t=1226693
http://www.computerbase.de/forum/showthread.php?t=1226652
http://www.computerbase.de/forum/showthread.php?t=1226624
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Tags: rehttpwwwlinkdeforumlinksregex
2条回答

把你的第二个循环放到第一个循环里。因为你想循环浏览每个链接上的评论,而不仅仅是一个。你知道吗

.........................
for link in links:
    htmlfile_2 = urllib2.urlopen(link)
    htmltext_2 = htmlfile_2.read()
    htmltext_3 = htmltext_2.replace("/forum/", "http://www.computerbase.de/forum/")
    regex_2 = '<a href="(.+?)" id="comments-link"'
    pattern_2 = re.compile(regex_2)
    links_2 = re.findall(pattern_2,htmltext_3)
    print( ' '.join(links_2) )
    for link_neu in links_2:
        htmlfile_neu = urllib2.urlopen(link_neu)
        htmltext_neu = htmlfile_neu.read()
        regex_neu = 'class="postcounter">#(.+?)<'
        pattern_neu = re.compile(regex_neu)
        links_neu = re.findall(pattern_neu,htmltext_neu)
        print( ' '.join(links_neu) )

输出(我假设没有以下数字的没有任何评论):

http://www.computerbase.de/
http://www.computerbase.de/
http://www.computerbase.de/news/2013-07/simcity-koennte-offline-modus-erhalten/
http://www.computerbase.de/news/2013-07/das-motorola-moto-x-wird-individualisierbar-sein/
http://www.computerbase.de/news/2013-07/goty-version-von-borderlands-2-kuendigt-sich-an/
http://www.computerbase.de/news/2013-07/monitor-laesst-nutzer-3d-objekte-erfuehlen/
http://www.computerbase.de/news/2013-07/quadratisch-und-flexibel-be-quiet-shadow-rock-2/
http://www.computerbase.de/news/2013-07/schwere-android-sicherheitsluecke-betrifft-alle-geraete/
http://www.computerbase.de/artikel/handys/2013/nokia-lumia-925-im-test/
http://www.computerbase.de/news/2013-07/amds-radeon-hd-7730-steht-kurz-vor-marktstart/
http://www.computerbase.de/news/2013-07/apple-stellt-ceo-der-marke-yves-saint-laurent-ein/
http://www.computerbase.de/news/2013-07/zwoelf-22-nm-atom-celeron-und-pentium-benannt/
http://www.computerbase.de/news/2013-07/das-nokia-lumia-1020-mit-41-mp-kamera-zeigt-sich/
http://www.computerbase.de/news/2013-07/nikon-kuendigt-450-mm-wafer-scanner-fuer-2015-an/
http://www.computerbase.de/news/2013-07/prism-regierung-gibt-sich-immer-noch-ahnungslos/
http://www.computerbase.de/news/2013-07/cooler-master-bringt-schlichte-mechanische-tastatur/
http://www.computerbase.de/news/2013-07/spieleklassiker-outcast-soll-wiederbelebt-werden/
http://www.computerbase.de/news/2013-07/opera-15-macht-vieles-anders/
http://www.computerbase.de/news/2013-07/htc-schliesst-android-4.2-fuer-one-s-aus/
http://www.computerbase.de/news/2013-07/erneut-faelschungen-von-office-2010-im-umlauf/
http://www.computerbase.de/news/2013-07/libreoffice-will-mit-amd-die-gpu-unterstuetzung-verbessern/
http://www.computerbase.de/news/2013-07/tablets-mit-firefox-os-in-vorbereitung/
http://www.computerbase.de/news/2013-07/nokia-bringt-feature-phones-mit-umts-fuer-70-dollar/
http://www.computerbase.de/news/2013-07/erstes-benchmarkergebnis-von-intels-bay-trail-t-aufgetaucht/
http://www.computerbase.de/news/2013-07/broken-age-wird-in-zwei-teilen-veroeffentlicht/
http://www.computerbase.de/news/2013-07/amd-plant-sparsame-t-modelle-von-richland/
http://www.computerbase.de/news/2013-07/fm2-mainboard-von-biostar-fuer-audiophile-mit-amd-apus/


http://www.computerbase.de/forum/showthread.php?t=1227204
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
http://www.computerbase.de/forum/showthread.php?t=1226943
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
http://www.computerbase.de/forum/showthread.php?t=1227135
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
http://www.computerbase.de/forum/showthread.php?t=1227123
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
http://www.computerbase.de/forum/showthread.php?t=1227120
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
http://www.computerbase.de/forum/showthread.php?t=1227113
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
http://www.computerbase.de/forum/showthread.php?t=1227077
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
http://www.computerbase.de/forum/showthread.php?t=1227036
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
http://www.computerbase.de/forum/showthread.php?t=1227062
1 2 3 4 5 6 7 8 9 10 11 12 13 14
http://www.computerbase.de/forum/showthread.php?t=1227058
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
http://www.computerbase.de/forum/showthread.php?t=1227047
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
http://www.computerbase.de/forum/showthread.php?t=1227037
1 2 3 4
http://www.computerbase.de/forum/showthread.php?t=1227016
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
http://www.computerbase.de/forum/showthread.php?t=1226986
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
http://www.computerbase.de/forum/showthread.php?t=1226977
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
http://www.computerbase.de/forum/showthread.php?t=1226962
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
http://www.computerbase.de/forum/showthread.php?t=1226946
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
http://www.computerbase.de/forum/showthread.php?t=1226913
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
http://www.computerbase.de/forum/showthread.php?t=1226872
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
http://www.computerbase.de/forum/showthread.php?t=1226820
1 2 3 4 5 6 7 8 9
http://www.computerbase.de/forum/showthread.php?t=1226818
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
http://www.computerbase.de/forum/showthread.php?t=1226753
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
http://www.computerbase.de/forum/showthread.php?t=1226693
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
http://www.computerbase.de/forum/showthread.php?t=1226652
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
http://www.computerbase.de/forum/showthread.php?t=1226624
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

相关问题 更多 >