【发布时间】:2020-01-22 05:05:55
【问题描述】:
我有一个数据库,其中包含不同书籍的 ISBN 编号。我使用 Python 和 Beautifulsoup 收集了它们。接下来,我想为书籍添加类别。书籍类别有一个标准。一个名为https://www.bol.com/nl/ 的网站拥有所有符合标准的书籍和类别。
起始网址:https://www.bol.com/nl/
国际标准书号:9780062457738
搜索后的网址:https://www.bol.com/nl/p/the-subtle-art-of-not-giving-a-f-ck/9200000053655943/
HTML 类别:<li class="breadcrumbs__item"
有谁知道如何 (1) 在搜索栏中输入 ISBN 值,(2) 然后提交搜索查询并使用页面进行抓取?
步骤 (3) 抓取所有类别是我可以做的事情。但我不知道如何执行前两个步骤。
到目前为止我为步骤 (2) 编写的代码
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
webpage = "https://www.bol.com/nl/" # edit me
searchterm = "9780062457738" # edit me
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get(webpage)
sbox = driver.find_element_by_class_name("appliedSearchContextId")
sbox.send_keys(searchterm)
submit = driver.find_element_by_class_name("wsp-search__btn tst_headerSearchButton")
submit.click()
到目前为止我为步骤 (3) 编写的代码
import requests
from bs4 import BeautifulSoup
data = requests.get('https://www.bol.com/nl/p/the-subtle-art-of-not-giving-a-f-ck/9200000053655943/')
soup = BeautifulSoup(data.text, 'html.parser')
categoryBar = soup.find('ul',{'class':'breadcrumbs breadcrumbs--show-last-item-small'})
for category in categoryBar.find_all('span',{'class':'breadcrumbs__link-label'}):
print(category.text)
【问题讨论】:
-
你的代码试验是什么,你得到了什么错误?
-
@Dev 我没有收到任何错误。我只是不知道从哪里开始。 (2)中的代码来自互联网,但我不知道如何正确使用 webdriver。你知道怎么做吗?
标签: python web-scraping beautifulsoup selenium-chromedriver