【问题标题】:Scraping Issue. Why no data is being scraped?刮问题。为什么没有数据被抓取?
【发布时间】:2021-08-08 08:11:06
【问题描述】:
import requests
from bs4 import BeautifulSoup

URL = 'https://www.colonialzone-dr.com/c-dominicanismos-dictionary'
page = requests.get(URL)

print("testing")
soup = BeautifulSoup(page.content, 'html.parser')

words = soup.find_all('p', class_="entry-content")
print(len(words))

for word in words:
    print(word.text)
    

// 我的控制台上没有显示任何内容,并且长度变量返回 0,这意味着没有任何内容被抓取。

【问题讨论】:

  • 该页面上没有 p 标记元素与 entry-content 类。
  • 所有单词都在 div 内的 p 标签元素中 class="entry-content" ||还是我在看东西?
  • 您只能给出具有相应类/id 属性的标签。在这种情况下,div 带有标签 entry-content

标签: python html css web-scraping


【解决方案1】:

您正在将附加参数作为条目内容传递给单词,但无需传递附加参数。

words = soup.find_all('p', class_="entry-content")

尝试代替那个,

words = soup.find_all('p')

然后它将获取所有带有 p 的内容并为您提供长度。

print(len(words))

希望对你有帮助..

【讨论】:

    【解决方案2】:

    没有带有条目内容的p标签?删除它,它应该可以工作

    import requests
    from bs4 import BeautifulSoup
    
    URL = 'https://www.colonialzone-dr.com/c-dominicanismos-dictionary'
    page = requests.get(URL)
    
    print("testing")
    soup = BeautifulSoup(page.content, 'html.parser')
    
    words = soup.find_all('p')
    print(len(words))
    
    for word in words:
        print(word.text)
    

    但是如果你想在 div 中包含“entry-content”类的 p 内容,那么

    entryContent=soup.find('div',attrs={"class":"entry-content"})
    words=entryContent.find_all("p")
    

    【讨论】:

      【解决方案3】:

      如果您看到 html,则其中存在所有 p 标签的 div,因此您可以将该 div 标签与关联类一起获取,然后从中获取 p 标签,以便您获得输出

      import requests
      from bs4 import BeautifulSoup
      
      URL = 'https://www.colonialzone-dr.com/c-dominicanismos-dictionary'
      page = requests.get(URL)
      
      print("testing")
      soup = BeautifulSoup(page.content, 'html.parser')
      
      main_div = soup.find('div', attrs={"class":"entry-content"})
      words=main_div.find_all("p")
                                         
      
      for word in words:
          print(word.text)
      

      输出:

      testing
      The slang used in Dominican Republic.
      C – ce
      *Caballo – person similar to a tigre but a little more decent
      *Cabron – a large male goat,also means displeased
      *Cacaito – candy
       ....
      

      【讨论】:

        猜你喜欢
        • 2022-11-29
        • 1970-01-01
        • 1970-01-01
        • 2011-03-15
        • 1970-01-01
        • 2016-12-19
        • 1970-01-01
        • 1970-01-01
        • 2020-03-29
        相关资源
        最近更新 更多