代码改变世界

day03:爬取京东商品信息

2019-07-03 21:49  FriendG  阅读(342)  评论(0)    收藏  举报
from selenium import webdriver  # 用来驱动浏览器的
from selenium.webdriver import ActionChains # 破解滑动验证码的时候用的 可以拖动图片
from selenium.webdriver.common.by import By # 按照什么方式查找,By.ID,By.CSS_SELECTOR
from selenium.webdriver.common.keys import Keys # 键盘按键操作
from selenium.webdriver.support import expected_conditions as EC # 和下面WebDriverWait一起用的
from selenium.webdriver.support.wait import WebDriverWait # 等待页面加载某些元素
import time


#js_code = '''
#window.
#'''
def get_good(driver):
num = 1
try:
time.sleep(5)
js_code = '''
window.scrollTo(0,5000)
'''
driver.execute_script(js_code)
time.sleep(5)
good_list = driver.find_elements_by_class_name('gl-item')
for good in good_list:

#printf(good)
#商品信息
good_name = good.find_element_by_css_selector('.p-name em').text

#print(good_name)

#商品链接
good_url = good.find_element_by_css_selector('.p-name a').get_attribute('href')

#商品价格
good_price = good.find_element_by_class_name('p-price').text
#print(good_price)

#商品评价
good_commit = good.find_element_by_class_name('p-commit').text

good_content = f'''
num:{num}
商品价格;{good_name}
商品链接:{good_url}
商品价格:{good_price}
商品评价:{good_commit}
\n
'''
print(good_content)
num+=1
with open('jd.txt','a',encoding='utf-8') as f:
f.write(good_content)
print('商品信息写入成功')

#找到下一页并点击
next_tag = driver.find_element_by_class_name('pn-next')
next_tag.click()
time.sleep(5)
get_good(driver)
finally:
driver.close()

if __name__=='__main__':
driver = webdriver.Chrome()
try:
driver.implicitly_wait(10)
# 往京东发送请求
driver.get('https://www.jd.com')

# 往京东主页输入框输入墨菲定律,按回车键
input_tag = driver.find_element_by_id('key')
input_tag.send_keys('墨菲定律')
input_tag.send_keys(Keys.ENTER)
get_good(driver)
finally:
driver.close()