[英]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.