Python:尝试抓取youtube时出错
当我试图在youtube的主页上搜索每个视频的标题时,我运行了这个代码Python:尝试抓取youtube时出错,python,web-scraping,python-requests,Python,Web Scraping,Python Requests,当我试图在youtube的主页上搜索每个视频的标题时,我运行了这个代码 import request from bs4 import BeautifulSoup url = 'https://www.youtube.com' html = requests.get(url) soup = BeautifulSoup(html.content, "html.parser") print(soup('a')) 它返回这个错误 Traceback (most recent call last):
import request
from bs4 import BeautifulSoup
url = 'https://www.youtube.com'
html = requests.get(url)
soup = BeautifulSoup(html.content, "html.parser")
print(soup('a'))
它返回这个错误
Traceback (most recent call last):
File "C:\Users\kenda\OneDrive\Desktop\Projects\youtube.py", line 7, in <
<module>
print(soup('a'))
File "C:\Users\kenda\AppData\Local\Programs\Python\Python36-
32\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\U0001f384' in
position 45442: character maps to <undefined>
[Finished in 4.83s]
我该如何解决这个问题?当我抓取youtube的时候,为什么要这么做呢?Urllib更好,使用起来也更舒适
from urllib.request import urlopen
from bs4 import BeautifulSoup
urlopen函数将url转换为html
url = 'https://www.youtube.com'
html = urlopen(url)
beautifulsoup将发布html
soup = BeautifulSoup(html, 'html.parser')
print(soup.find_all('a'))
如果您确实想处理请求,解决方案是:
import requests
from bs4 import BeautifulSoup
url = 'https://www.youtube.com'
resp = requests.get(url)
html = resp.text
soup = BeautifulSoup(html, 'html.parser')
print(soup.find_all('a'))
看起来你想打印圣诞树表情符号或类似的东西,我猜。。。使用错误的编码设置。您已经看过这个了吗:?请求模块要简单得多,我只想弄清楚如何做到这一点