在使用请求和精美汤抓取时无法提取描述和评分

时间:2018-12-13 11:48:25

标签: python python-3.x web-scraping beautifulsoup

我是网络抓取的初学者,我正在抓取这个特定的网页https://myanimelist.net/anime/394,在这里我无法通过使用请求和Beautiful Soup的python代码来获取描述和评级。该代码对于上述url索引的其他页面正常工作。当同一代码可用于其他页面时,找不到代码中的错误。

到目前为止,我在代码中的进步是:

from bs4 import BeautifulSoup
import requests


url="https://myanimelist.net/anime/394"
source=requests.get(url)
soup=BeautifulSoup(source.content,'lxml')


def info_anime(soup):

    #Extracting the name of the anime

    anime=soup.find(name="span",attrs={"itemprop":"name"})
    name=anime.text
    print ("Anime : "+name)

    #Extracting the rating 

    rating=soup.find(name="div",attrs={"class":"fl-l score"})
    print ("Rating : "+(rating.text.strip()))


    #extracting the description

    des=soup.find(name="span",attrs={"itemprop":"description"})
    description=des.text
    print ("Description : "+description)

    #Extracting the Rank

    rank=soup.find(name="span",attrs={"class":"numbers ranked"})
    print (rank.text)

    #Extracting number of episodes

    ep=soup.find(name="div",attrs={"class":"spaceit"})
    print (ep.text)

print (info_anime(soup))

1 个答案:

答案 0 :(得分:2)

更改:

soup=BeautifulSoup(source.content,'lxml')

收件人:

soup=BeautifulSoup(source.content,'html.parser')

当我更改它时,我得到了输出:

Anime : Ai Yori Aoshi: Enishi
Rating : 7.22
Description : Two years after meeting Aoi, Kaoru and gang are still up to their normal habits. Kaoru now in grad school and the tenants being as rowdy as ever what will become of Aoi and Kaoru's love.

Two years has passed since Aoi and Kaoru were freed from the bonds of their families. They continue to live their normal lives with their usual friends in their house.
Ranked #2737

Episodes:
  12

None