2015-06-30 18 views
0

我試圖製作一個腳本,它讀取crunchyroll的rss並訪問LINK中的最新上傳和下載信息。 過程如下所示: 1.)閱讀來自RSS的最新一集鏈接。 2.)轉到鏈接 3.)在源代碼中,查找文本「ssid」。 4.)獲取ssid的6個字符。 5.)然後將這些字符追加到「http://www.crunchyroll.com/xml/?req=RpcApiSubtitle_GetXml&subtitle_script_id=」的末尾,並保存xml頁面。如何在python的請求庫中使用變量

我的腳本可以半路上......

我的代碼: -

import feedparser 
import webbrowser 
import os 
import subprocess 
import re 
import urllib 
import urllib2 
from urllib2 import urlopen 
from bs4 import BeautifulSoup 
import requests 
import cookielib 


feed = feedparser.parse('http://www.crunchyroll.com/rss/anime') #checks the RSS 
url = feed['entries'][0]['link'] + '?p720=1'   # get's the link from latest release and appends some character for the 720p resolution of the link. 

# Now, here, I'm writing this URL to a text file and then read from the text file 

file = open("newfile.txt", "w") 
file.write(url) 
file.close() 

file = open('newfile.txt', 'r') 
#print file.read() 
lobo = file.read() 
print lobo 

# Now, I put the URL that is being read from file in requests to go to the link. Everything works fine till here. 

r = requests.get(lobo) 
soup = BeautifulSoup(r.text) 
print soup.title 
webbrowser.open_new_tab(lobo) 
subtitles = soup.findAll('span',{'class':'showmedia-subtitle-text'}) 
for ssid in subtitles: 
    x = ssid.find_all('a', limit=1) 
for a in x: 
    print a['href'][-6:] 

xmlLink = 'http://www.crunchyroll.com/xml/?req=RpcApiSubtitle_GetXml&subtitle_script_id=' + a['href'][-6:] 
#webbrowser.open_new_tab(xmlLink) 
print xmlLink 

現在,我得到錯誤 'A' 在這個xmlLink沒有定義。

但是,它有一個轉折...如果我把直接的http鏈接放在「r = requests.get(lobo)」..一切都像它應該的那樣工作。但是,如果我使用這個變量。它不工作。

任何幫助將是appreciated.Thank您

回答

0

它看起來像a變量在for循環的內部定義,但xmlLink變量不是。嘗試縮進xmlLink行以匹配for循環的縮進級別。例如:

for a in x: 
    print a['href'][-6:] 

    xmlLink = 'http://www.crunchyroll.com/xml/?req=RpcApiSubtitle_GetXml&subtitle_script_id=' + a['href'][-6:] 
    #webbrowser.open_new_tab(xmlLink) 
    print xmlLink 
+0

好,它去掉了錯誤,但是,問題是,SSID的值不能分析在結尾的鏈接...不知道爲什麼... – Xonshiz

0

您使用的網址是str。你應該使用Python的字符串格式函數。

xmlLinkBase = 'http://www.crunchyroll.com/xml/?req=RpcApiSubtitle_GetXml&subtitle_script_id={0}' 
for a in x: 
    print a['href'][-6:] 
    xmlLink = xmlLinkBase.format(a['href'][-6:]) 
    #webbrowser.open_new_tab(xmlLink) 
    print xmlLink 

str.formatDocs