【问题标题】:AttributeError: 'str' object has no attribute 'descendants' - BeautifulSoup4AttributeError:“str”对象没有属性“后代”-BeautifulSoup4
【发布时间】:2021-02-03 05:22:16
【问题描述】:

我想抓取特定 url = https://www.sortlist.fr/pub 中的 s 并选择找到以下值的特定一个 =“Dupont Lewis”。我正在使用以下代码:

import requests
from bs4 import BeautifulSoup
r = requests.get("https://www.sortlist.fr/pub")
BeautifulSoup.find_all("a")
BeautifulSoup("a")
search = BeautifulSoup.select_one("a[title*=Dupont Lewis]")
if len(search)>0:
    print ('I find')
else:
    print ('None')

但我收到以下错误="AttributeError: 'str' object has no attribute 'descendants'"

谁能帮帮我?

【问题讨论】:

标签: python beautifulsoup


【解决方案1】:

错误是您没有从服务器响应中创建汤:

import requests
from bs4 import BeautifulSoup

r = requests.get("https://www.sortlist.fr/pub")
soup = BeautifulSoup(r.content, 'html.parser')          # <-- create a soup
search = soup.select_one('a[title*="Dupont Lewis"]')    # <-- put "Dupont Lewis" inside ""
if search:                                              # <-- len() isn't necessary, because of .select_one
    print ('I find')
else:
    print ('None')

打印:

I find

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-22
    • 1970-01-01
    • 2018-09-10
    • 2021-10-04
    • 2019-12-02
    • 2021-09-25
    相关资源
    最近更新 更多