嗨,我是 python 編程的新手。我嘗試使用 python 抓取一個新聞網站。我得到了標題及其鏈接。但是當我嘗試將其保存在 excel 檔案中時,它顯示值錯誤
這是源代碼和錯誤
import requests, openpyxl
from bs4 import BeautifulSoup
excel = openpyxl.Workbook()
sheet = excel.active
sheet.title = 'Maalaimalar Links'
sheet.append(['Title','Link'])
req = requests.get("https://www.maalaimalar.com/news/topnews/1")
head_lines = BeautifulSoup(req.text, 'html.parser')
hliness = head_lines.find_all('div', class_ = 'col-md-4 article')
for hlines in hliness:
h2lines = hlines.find('h3').text
link = hlines.find('a')
print(h2lines)
print(link.get('href'))
sheet.append([h2lines, link])
excel.save('maalaimalar.xlsx')
這是我用這一行執行時的錯誤
sheet.append([h2lines, link])
ValueError: Cannot convert <a href="https://www.maalaimalar.com/news/topnews/2022/03/06182721/3549285/IPL-2022-Schedule-match-details-for-Chennai-super.vpf"><h3>?????? 2022 ???????- ?????? ??? ?????? ????????? ??????</h3></a> to Excel.
uj5u.com熱心網友回復:
您正在嘗試將BeautifulSoup物件推送到您的 excel 中,而不是提取href如下print(link.get('href')):
link = hlines.find('a').get('href')
要么
link = hlines.a.get('href')
例子
import requests, openpyxl
from bs4 import BeautifulSoup
excel = openpyxl.Workbook()
sheet = excel.active
sheet.title = 'Maalaimalar Links'
sheet.append(['Title','Link'])
req = requests.get("https://www.maalaimalar.com/news/topnews/1")
head_lines = BeautifulSoup(req.text, 'html.parser')
hliness = head_lines.find_all('div', class_ = 'col-md-4 article')
for hlines in hliness:
h2lines = hlines.find('h3').text
link = hlines.find('a').get('href')
sheet.append([h2lines, link])
excel.save('maalaimalar.xlsx')
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/438698.html
標籤:python-3.x 美丽的汤 蟒蛇请求 打开pyxl 值错误
上一篇:PostgresSQLAlchemy:附加引數應命名為<方言名稱>_<引數>,得到'ForeignKey'
下一篇:處理堆疊的合并表
