我想將我的 YouTube 訂閱串列廢棄到一個 csv 檔案中。我輸入了這個代碼(但我還沒有完成編碼):
import requests
from bs4 import BeautifulSoup
import csv
url = 'https://www.youtube.com/feed/channels'
source = requests.get(url)
soup = BeautifulSoup(source, 'lxml')
我發現了這個錯誤:
檔案“/Users/hendy/YouTube 訂閱scraping.py”,第7 行,湯= BeautifulSoup(source, 'lxml') 檔案“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site -packages/bs4/ init .py", line 312, in init elif len(markup) <= 256 and ( TypeError: object of type 'Response' has no len()
我不知道有什么問題。
uj5u.com熱心網友回復:
怎么了?
您使用整個response物件并將其推到BeautifulSoup不起作用的地方。
怎么修?
要生成BeautifulSoup物件,請使用回應的content或text:
BeautifulSoup(source.content, 'lxml')
例子
from bs4 import BeautifulSoup
import requests
headers ={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'}
url = 'https://www.youtube.com/feed/channels'
source = requests.get(url, headers=headers)
soup = BeautifulSoup(source.content, 'lxml')
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/389813.html
下一篇:使用Scrapy傳遞請求
