爬取站点
任意一个携程酒店的详细链接,这里给出了四个,准备开四个线程爬取:
https://hotels.ctrip.com/hotel/6278770.html#ctm_ref=hod_hp_hot_dl_n_2_7
https://hotels.ctrip.com/hotel/6657909.html#ctm_ref=hod_hp_hot_dl_n_2_8
https://hotels.ctrip.com/hotel/441351.html#ctm_ref=hod_hp_hot_dl_n_2_1
https://hotels.ctrip.com/hotel/5470972.html#ctm_ref=hod_hp_hot_dl_n_2_3
准备工作
Python版本:Python3.6
安装selenium模块:
pip3 install selenium
安装lxml模块:
pip3 install lxml
使用到的知识
selenium模块的使用
Xpath的使用
多线程
IO
代码
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
import time
import os
from lxml import etree
import threading
import base64
# 爬取一个站点的评论信息
class ScrapyOne(object):
def __init__(self, url):
super(ScrapyOne, self).__init__()
# 站点url
self.url = url
# 当前爬取的页
self.page = 1
# 评价总页数
self.allPage = 1
# 使用Chrome浏览器
self.driver = webdriver.Chrome()
# 重新点击次数
self.retryNum = 5
# 翻页
def nextPage(self):
try:
self.page += 1
# 一下的代码可能由于页面的刷新报错,因此这里使用了try-catch
# 清空页码输入框
self.driver.find_elements_by_id("cPageNum")[0].clear()
# 输入下一页的页码
self.driver.find_elements_by_id("cPageNum")[0].send_keys(self.page)
# 点击翻页按钮
self.driver.find_elements_by_id('c