繁体   English   中英

Beautifulsoup Python 无法从网站上抓取数据

[英]Beautifulsoup Python unable to scrape data from a website

我一直在使用 Python Beautifulsoup 来抓取数据。 至此已经成功刷屏。 但坚持以下网站。

目标网站: LyricsHindiSong

我的目标是从上述网站上抓取歌词。 但它一直给出空白结果或 Nonetype object 没有属性类型错误。

自过去 15 天以来一直在苦苦挣扎,无法弄清楚问题出在哪里以及如何解决?

以下是我正在使用的代码。

import pymysql
import requests
from bs4 import Beautifulsoup

r=requests.get("https://www.lyricshindisong.in/2020/04/chnda-re-chnda-re-chhupe-rahana.html")
soup=Beautifulsoup(r.content,'html5lib')
pageTitle=soup.find('h1').text.strip()
targetContent=soup.find('div',{'style':'margin:25px; color:navy;font-size:18px;'})
print(pageTitle)
print(targetContent.text.strip())

它打印错误 nonetype object has no text error。 如果我检查检查 window,元素同时存在两个元素。 无法理解问题出在哪里。 至少它应该打印标题页。

希望你能理解我的要求。 请指导我。 谢谢。

您在bs4 lib 的 class 名称中犯了一个错误,并使用了find方法而不是find_all

完整代码:

import requests
from bs4 import BeautifulSoup


url = "https://www.lyricshindisong.in/2020/04/chnda-re-chnda-re-chhupe-rahana.html"
response = requests.get(url)

soup = BeautifulSoup(response.content,'html5lib')

title = soup.find('h1').text.strip()
content = soup.find_all('div',{'style':'margin:25px; color:navy;font-size:18px;'})

print(title)

for line in content:
    print(line.text.strip())

结果:

python answer.py
Chnda Re Chnda Re Chhupe Rahana
चंदा रे, चंदा रे, छुपे रहनासोये मेरी मैना, लेके मेरी निंदिया रे
फूल चमेली धीरे महको, झोका ना लगा जाये नाजुक डाली कजरावाली सपने में मुस्काये लेके मेरी निंदिया रे
हाथ कहीं है, पाँव कहीं है, लागे प्यारी प्यारी ममता गाए, पवन झुलाये, झूले राजकुमारी लेके मेरी निंदिया रे  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM