现在我正在处理我的第一个python项目。假设从YouTube搜索中随机提取一个视频,然后将其添加到Watch2Gether。现在,我的代码如下:
# Take a random video from my youtube recommended and add it to watch2gether
import requests
from bs4 import BeautifulSoup as BS
import time
import random
from fake_useragent import UserAgent
# Importing libraries
def GetVideos(): # Function to generate videos from youtube
num = random.randint(1, 20)
recommended = requests.get('https://www.youtube.com/results?search_query=svenska+youtube+klassiker&sp=EgIQAQ%253D%253D')
recommended_soup = BS(recommended.content, features='lxml')
all_links = recommended_soup.find_all('a', href=True)
my_links = [] #Just a empty list to story all /watch links
for link in all_links:
if '/watch' in link['href']:
print(link['href'])
my_links.append(link['href'])
#check for all the /watch and appends them to my emty list
print('www.youtube.com' + my_links[num])
ua = UserAgent()
print(ua.random)
URL = "https://www.watch2gether.com/rooms/zgabjnblvqy52vam0c"
r = requests.get(URL, data={'class': 'w2g-messages-input'})
# And done.
print(r.text) # displays the result body.
我的目标是使我的程序加入我的“房间”,然后添加随机视频。我已经找到了如何获取视频的所有链接,但无法将它们发布到“房间”中。感谢所有帮助! :)如果您需要更多信息,请发表评论。
答案 0 :(得分:0)
如评论中所述,首先要做的是将requests.get
更改为requests.post
。
我也建议您将data={'class': 'w2g-messages-input'}
更改为json={'class': 'w2g-messages-input'}
。
因此您发送请求的代码行将如下所示:
r = requests.post(URL, json={'class': 'w2g-messages-input'})
希望这会有所帮助。