问题遇到的现象和发生背景
在爬取京东商品信息时,相同的操作,爬取关键字为‘苹果13’时正常出结果,但是到这里的口红时就不可以了
运行debug时发现goods的列表为空
问题相关代码,请勿粘贴截图
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
def information_kouhong(url,keyword):
driver = webdriver.Chrome()
driver.get(url)
#搜索
imput_tag = driver.find_element(By.ID,'key')
imput_tag.send_keys(keyword)
imput_tag.send_keys(Keys.ENTER)
#商品详情
get_goods(driver)
def get_goods(driver):
goods = driver.find_elements(By.CLASS_NAME,'gl-item')
for good in goods:
herf=good.driver.find_element(By.TAG_NAME,'a').get_attribute('herf')
src=good.driver.find_element(By.TAG_NAME,'a').get_attribute('src')
price=good.driver.find_element(By.CSS_SELECTOR,'.p-price')
msg='''
链接:%s
图片:%s
价格:%s
'''%(herf,src,price)
print(msg)
button = driver.find_element(By.PARTIAL_LINK_TEXT, '下一页') # 定位文本信息
button.click()
time.sleep(1)
get_goods(driver)
if __name__ == "__main__":
information_kouhong('https://www.jd.com/','口红')