简体   繁体   English

如何修复AttributeError:'HTMLParserTreeBuilder'对象没有属性'initialize_soup'

[英]how to fix AttributeError: 'HTMLParserTreeBuilder' object has no attribute 'initialize_soup'

I am getting the following error: 我收到以下错误:

AttributeError: 'HTMLParserTreeBuilder' object has no attribute 'initialize_soup'

I was trying to find the xpath for the checkbox of M in eBay ( link ) 我试图在eBay中找到M复选框的xpath( 链接

I am using spyder and I have imported bs4 everywhere I can. 我正在使用spyder,并且已尽可能导入bs4。

import requests
from bs4 import BeautifulSoup

web_page = requests.get('https://www.ebay.com/sch/i.html?_from=R40&_trksid=m570.l1313&_nkw=mens+shirt&_sacat=0')
web_soup = BeautifulSoup(web_page.text, 'html.parser')
checkbox = soup.find(class_='cbx x-refine__multi-select-checkbox')
checkbox_names = checkbox.find_all('a')

for check in checkbox_names:
    print(check.prettify())

I was expecting an output like this 我期待这样的输出

<a href="/web/20121007172955/https://www.nga.gov/cgi-bin/tsearch?artistid=11630">
 Zabaglia, Niccola
</a>

I was following this tutorial to help me with my code. 我正在遵循本教程来帮助我编写代码。

First, your soup.find(class_='cbx x-refine__multi-select-checkbox') is selecting actually only the first element with the class cbx x-refine__multi-select-checkbox 首先,您的soup.find(class_='cbx x-refine__multi-select-checkbox')实际上仅选择具有类cbx x-refine__multi-select-checkbox的第一个元素

So to get the url to the "M Size Shirt", you could do the following: 因此,要获取“ M尺寸衬衫”的网址,您可以执行以下操作:

CODE: 码:

import requests
from bs4 import BeautifulSoup as soup

web_page = requests.get('https://www.ebay.com/sch/i.html?_from=R40&_trksid=m570.l1313&_nkw=mens+shirt&_sacat=0')
web_soup = soup(web_page.text, 'html.parser')
links = web_soup.find_all('a', {'class':'cbx x-refine__multi-select-link'})
for l in links:
    checkbox = l.find('input',{'class':'cbx x-refine__multi-select-checkbox '})
    if checkbox and 'M' == checkbox.get('aria-label'):
        #FOUND
        print(l.get('href'))

OUTPUT: 输出:

https://www.ebay.com/sch/i.html?_from=R40&_nkw=mens+shirt&_sacat=0&rt=nc&Size%2520%2528Men%2527s%2529=M&_dcat=185100

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Beautiful Soup 4抓取HTML网址修复“ AttributeError:&#39;客户端&#39;对象没有属性&#39;send_message&#39;” - How to fix “AttributeError: 'Client' object has no attribute 'send_message'” with Beautiful Soup 4 scraping HTML url 如何修复:AttributeError:Class对象没有属性 - How to fix: AttributeError:Class object has no attribute AttributeError: &#39;str&#39; 对象没有属性 &#39;soup&#39; - AttributeError: 'str' object has no attribute 'soup' AttributeError:类型 object 'database' 没有属性 'initialize' - AttributeError: type object 'database' has no attribute 'initialize' 如何修复 AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;click&#39; - How to fix AttributeError: 'NoneType' object has no attribute 'click' 如何修复“AttributeError: &#39;str&#39; object has no attribute &#39;content&#39;”python 错误 - how to fix for the "AttributeError: 'str' object has no attribute 'content' "python error 如何修复AttributeError:&#39;NoneType&#39;对象没有属性&#39;theme_cls&#39; - How to fix AttributeError: 'NoneType' object has no attribute 'theme_cls' 如何修复 AttributeError: &#39;bytes&#39; object has no attribute &#39;encode&#39;? - How do I fix AttributeError: 'bytes' object has no attribute 'encode'? 如何修复“”AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;app&#39; - How to fix ""AttributeError: 'NoneType' object has no attribute 'app' 如何修复:“AttributeError:&#39;Class&#39; 对象没有属性 &#39;rect&#39;” - How to fix: "AttributeError: 'Class' object has no attribute 'rect'"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM