2016-07-17 13 views
0

Pythonで文字列置換を使用しようとしていますが、機能しません。文字を空白のスペースに置き換えようとしています。私のコードをheres。Pythonの文字列置換が機能しません

def panties(): 
     pan_url = 'http://www.panvideos.com' 
     html = requests.get(pan_url, headers=headers) 
     soup = BeautifulSoup(html.text, 'html5lib') 
     video_row = soup.find_all('div', {'class': 'video'}) 

     def youtube_link(url): 
      youtube_page = requests.get(url, headers=headers) 
      soupdata = BeautifulSoup(youtube_page.text, 'html5lib') 
      video_row = soupdata.find('div', {'class': 'video-player'}) 
      entries = [{'text': str(div), 
         } for div in video_row][3]['text'] 

      removed = '<script type="text/javascript">jwplayer("video-setup").setup(' 
      newstring = entries.replace(removed, "") 


      return newstring 

     entries = [{'text': div.h4.text, 
        'href': div.a.get('href'), 
        'tube': youtube_link(div.a.get('href')), 
        } for div in video_row][:1] 

     return entries 

が、それはまだ私が間違って何をやっている

<script type="text/javascript">jwplayer("video-setup").setup({file:"http://www.youtube.com/watch?v=jucBuAzuZ0E",image:"http://i1.ytimg.com/vi/jucBuAzuZ0E/maxresdefault.jpg",primary:"html5",stretching:"fill","controlbar":"bottom",width:"100%",aspectratio:"16:9",autostart:"true",logo:{file:"http://www.panvideos.com/uploads/bien-png578aab16676e1.png",position:"bottom-right",link:"http://www.panvideos.com/"},sharing:{link:"http://www.panvideos.com/video/3020/alejandro-sanz-deja-que-te-bese-ft-marc-anthony-official-video-/","sites":["facebook","twitter","linkedin","pinterest","tumblr","googleplus","reddit"]}});</script> 

代わりの

{file:"http://www.youtube.com/watch?v=jucBuAzuZ0E",image:"http://i1.ytimg.com/vi/jucBuAzuZ0E/maxresdefault.jpg",primary:"html5",stretching:"fill","controlbar":"bottom",width:"100%",aspectratio:"16:9",autostart:"true",logo:{file:"http://www.panvideos.com/uploads/bien-png578aab16676e1.png",position:"bottom-right",link:"http://www.panvideos.com/"},sharing:{link:"http://www.panvideos.com/video/3020/alejandro-sanz-deja-que-te-bese-ft-marc-anthony-official-video-/","sites":["facebook","twitter","linkedin","pinterest","tumblr","googleplus","reddit"]}});</script>  

を返しますか?

+1

私が見ることができる 'replace'呼び出しには何も問題はありません。あなたの入力例が期待通りに機能しているので、他の要素をチェックする必要があります。あなたの入力をあなたの削除されたテキストと比較するために、デバッグの前と後の呼び出しをデバッグしてください。 – Gavin

+0

@Gavin私は最初に結果を文字列に変換しなければならなかったので、replaceメソッドを使用することができました。返信ありがとう – losee

答えて

0

私はそう

oldstring = str(entries) 
removed = '<script type="text/javascript">jwplayer("video-setup").setup(' 
newstring = oldstring.replace(removed, "") 

return newstring 

それが動作するような文字列にエントリを変換しなければならなかったことを考え出しました。返されるのは文字列ではないと思います。

関連する問題