我收到以下错误:
AttributeError: 'HTMLParserTreeBuilder' object has no attribute 'initialize_soup'
我试图在eBay(link)中找到M复选框的xpath
我正在使用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())
我期待这样的输出
<a href="/web/20121007172955/https://www.nga.gov/cgi-bin/tsearch?artistid=11630">
Zabaglia, Niccola
</a>
我遵循this tutorial来帮助我编写代码。
最佳答案
首先,您的soup.find(class_='cbx x-refine__multi-select-checkbox')
实际上只选择类cbx x-refine__multi-select-checkbox
的第一个元素
因此,要获取“ M尺寸衬衫”的网址,您可以执行以下操作:
码:
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'))
输出:
https://www.ebay.com/sch/i.html?_from=R40&_nkw=mens+shirt&_sacat=0&rt=nc&Size%2520%2528Men%2527s%2529=M&_dcat=185100
关于python - 如何修复AttributeError:'HTMLParserTreeBuilder'对象没有属性'initialize_soup',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56468041/