Beautifulsoup Python unable to scrape data from a website

别说谁变了你拦得住时间么 提交于 2021-02-19 01:49:25

问题


I have been using Python Beautifulsoup to scrape data. So far have beeen successfully scraped. But stuck with the following website.

Target Site: LyricsHindiSong

My goal is scrape song lyrics from the mentioned website. But all the time it gives blank result or Nonetype object has no attribute kind error.

Have been struggling since last 15 days and could not able to figure out where was the problem and how to fix it?

Following is the code which is I am using.

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())

It prints error nonetype object has no text error. If I check in the inspect window, element has both the elements present. Unable to understand where is the problem. Atleast it should have printed the title page.

Hope you understand my requirement. Please guide me. Thanks.


回答1:


You made a mistake in class name from bs4 lib and used find method instead of find_all

Full code:

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())

Result:

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


来源:https://stackoverflow.com/questions/61158222/beautifulsoup-python-unable-to-scrape-data-from-a-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!