我正在编写一些代码来解析XML文件。我只是想知道是否有人可以解释为什么这不起作用。如果我将link
本身放入urllib.urlopen()
,它似乎不会成为该网址。但是,当我将"http://gdata.youtube.com/feeds/api/standardfeeds/top_rated?max- results=50&time=today"
放在urllib.urlopen()
内时,它可以正常工作。它需要是一个字符串而不是变量,还是有办法绕过它?
import urllib
from bs4 import BeautifulSoup
class Uel(object):
def __init__(self, link):
self.content_data = []
self.num_likes = []
self.num_dislikes = []
self.favoritecount = []
self.view_count = []
self.link = link
self.web_obj = urllib.urlopen(link)
self.file = open('youtubequery.txt', 'w+')
self.file.write(str(self.web_obj))
for i in self.web_obj:
self.file.write(i)
with open("youtubequery.txt", "r") as myfile:
self.file_2=myfile.read()
self.soup = BeautifulSoup(self.file_2)
for link in self.soup.find_all("content"):
self.content_data.append(str(link.get("src")))
for stat in self.soup.find_all("yt:statistics"):
self.favoritecount.append(str(stat.get("favoritecount")))
for views in self.soup.find_all("yt:statistics"):
self.view_count.append(str(views.get("viewcount")))
for numlikes in self.soup.find_all("yt:rating"):
self.num_likes.append(str(numlikes.get("numlikes")))
for numdislikes in self.soup.find_all("yt:rating"):
self.num_dislikes.append(str(numdislikes.get("numdislikes")))
def __str__(self):
return str(self.content_data),str(self.num_likes), str(self.num_dislikes)
link = "http://gdata.youtube.com/feeds/api/standardfeeds/top_rated?max- results=50&time=5"
data = Uel(link)
print data.__str__()
答案 0 :(得分:0)
在您提供的代码中,您正在使用此网址:
http://gdata.youtube.com/feeds/api/standardfeeds/top_rated?max- results=50&time=5
产生的请求:
Invalid value for time parameter: 5
但是,在问题本身中,您提到了以下网址:
http://gdata.youtube.com/feeds/api/standardfeeds/top_rated?max- results=50&time=today
有time=today
。带有此URL的代码适用于我。