Why does error 'NoneType' object has no attribute 'contents' occur with only one of two similar commands?

前端 未结 1 560
-上瘾入骨i
-上瘾入骨i 2021-01-28 11:11

I\'m extracting content from this url.

import requests
from bs4 import BeautifulSoup

url = \'https://www.collinsdictionary.com/dictionary/french-english/aimer\'
         


        
相关标签:
1条回答
  • 2021-01-28 11:50

    try this:

    import requests
    from bs4 import BeautifulSoup
    
    url = 'https://www.collinsdictionary.com/dictionary/french-english/aimer'
    headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0'}
    soup = BeautifulSoup(requests.get(url, headers = headers).content, 'html.parser')
    
    for script in soup.select('script, .hcdcrt, #ad_contentslot_1, #ad_contentslot_2'):
        script.extract()
    
    entry_name = soup.h2.text
    content1 = ''.join(map(str, soup.select_one('.cB.cB-def.dictionary.biling').contents))
    

    When you select classes and it is blank-spaces in it you replace the space with ..

    cB, cB-def, dictionary and biling is four different classes. And if you let the spaces be there the script looking for a tag with class cB-def inside of a tag with class cB and so on....

    0 讨论(0)
提交回复
热议问题