-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSeleniumSpider.py
63 lines (56 loc) · 2.08 KB
/
SeleniumSpider.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# /usr/bin/python
# -*- coding: utf-8 -*-
# @Time : 2019/12/10
# @Author : jwy
# @Email : [email protected]
# @Description : 根据游记链接使用Selenium模拟下拉操作爬取游记内容
from selenium import webdriver
import time
import random
class SeleniumSpider:
urlList = []
def __init__(self, path):
self.driver = webdriver.Chrome()
f = open(path, "r")
self.urlList = f.readlines()
f.close()
def scrapyYj(self):
i = 1
for url in self.urlList:
self.driver.get(url)
# 等待界面加载完成
# 可以改进
time.sleep(5)
# 滚动到页面最底部
self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
time.sleep(5)
self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
# time.sleep(1)
# get specific <p>
time.sleep(5)
# two kinds paragrph, one paper only have one kind
ps1 = self.driver.find_elements_by_xpath("//p[@class='_j_note_content _j_seqitem']")
ps2 = self.driver.find_elements_by_xpath("//p[@class='_j_note_content']")
time.sleep(5)
# 提取内容并保存到本地
content = ""
for p in ps1:
# remove space and \n
content = content + p.text.strip().replace("\n", "").replace(" ","")
for p in ps2:
# remove space and \n
content = content + p.text.strip().replace("\n", "").replace(" ","")
self.yjContentUtil(str(i), content)
# self.driver.close()
print("Yj: "+str(i))
print(content)
i = i + 1
time.sleep(random.randint(0, 1))
def yjContentUtil(self, name, content):
f = open("contents/"+name+".txt", "w", encoding='utf-8')
f.write(content)
f.close()
if __name__ == "__main__":
# 爬取游记内容
seleniumSpider = SeleniumSpider("ygUrls.txt")
seleniumSpider.scrapyYj()