from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
import time
from bs4 import BeautifulSoup
import csv
f0 = open('phone.csv', 'w', encoding='utf-8', newline='')
w0 = csv.writer(f0)
w0.writerow(['价格', '名称', '评论数', '店铺名', '产品网址', '店铺网址'])
f0.close()
b = Chrome()
b.get('https://www.jd.com/')
search = b.find_element(By.ID, 'key')
search.send_keys('手机\n')
time.sleep(2)
for x in range(10):
b.execute_script('window.scrollBy(0, 800)')
time.sleep(1)
time.sleep(2)
soup = BeautifulSoup(b.page_source, 'lxml')
div_list = soup.select('.gl-warp>li>div')
for x in div_list:
a = x.select_one('.p-price>strong>i').text
b2 = x.select_one('.p-name>a>em').text
c = x.select_one('.p-commit>strong>a').text
d = x.select_one('.p-shop>span>a').text
e1 = x.select_one('.p-name>a')
e2 = e1.attrs['href']
f1 = x.select_one('.p-shop>span>a')
f2 = f1.attrs['href']
print(a, b2, c, d, e2, f2)
with open('phone.csv', 'a', encoding='utf-8', newline='') as f:
w1 = csv.writer(f)
w1.writerow([a, b2, c, d, e2, f2])
time.sleep(1)
selenium作业
最新推荐文章于 2024-07-17 21:01:12 发布