这个网站为了反爬虫,使用了很多策略,而这些策略都会是爬虫中可能遇到的坑。
目的: 目的是爬取这个网页中的这个状态的项目(projectStatus=6
)(估计是表示结束了的意思)
"标的公司", "项目公司"
也是一样的道理"标的估值", "项目公司估值"
:
(中文输入法的:
),但是,在后面有些网页就会是:
(英文输入法的冒号加上空格作为分隔符)来表示的,但是后来发现,会随机在某两种数据直接,使用
(换行符来分隔)
tempDatas = []
for d in datas:
try:
td = d.find_element_by_tag_name('span').get_attribute("innerHTML")
if '
' in td:
tds = td.split('
')
for tdi in tds:
tempDatas.append(tdi.replace('\n', '').replace(' ', ' ').replace(': ', ':').replace(':', ":").strip())
else:
tempDatas.append(d.text.strip().replace('\n', '').replace(': ', ':').replace(':', ":"))
except Exception as e:
tempDatas.append(d.text.strip().replace('\n', '').replace(': ', ':').replace(':', ":"))
datas = tempDatas
[-1]
这样的方式来进行索引。具体的代码片段:
refresh
表示是否刷新cookiesif refresh:
browser.get(LOGIN_URL)
# 登录
time.sleep(2)
input = browser.find_element_by_xpath('//input[@placeholder="请输入手机号"]')
input.send_keys(USERNAME)
time.sleep(40)
pickle.dump(browser.get_cookies(), open("cookies.pkl", "wb"))
print('finish refresh')
browser.get(tempURL) # 加载网页
time.sleep(2)
else:
browser.get(LOGIN_URL) # 加载网页
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
browser.add_cookie(cookie)
browser.get(tempURL) # 加载网页
time.sleep(2)
类似的有切换网页的时候也会出现失败,但是刷新一下也就好了。这个我就限制了,到了一定次数就直接终止整个程序。因为,我有天晚上爬取,但是半夜的时候断网了… 发现一直在爬取空数据。所以就有了这样的设计。
同时,我也增加了等待机制。 类似于下面的这种。
elemnt = WebDriverWait(browser, 10).until(
EC.presence_of_element_located(
(By.XPATH, '//*[@id="__layout"]/div/div[2]/div/div[3]/ul/li[%d]/div/div/div[1]/div[2]/h3' % (
index_i + 1)))
)
同样是因为那次停电,我之前爬取到不少有用的数据,但是没保存,所以我加入了一个中途定次数记录的代码。来保持中间的数据,为避免突然发生的问题。
pageCount
第几页(是最开始的图片所示)index_i
在第几页的第几个(从0开始计数)用这个可以做分段爬取(有时候遇到问题了,对于某些部分可以用这个来设置重新开始)
refresh = False
为True的时候,需要手动登录。然后,等待出现刷新完成之后就说明已经保存好cookies了。然后以后再启动的时候,就设置为False。这样就不需要再登录了。
configure 这个部分的代码,一般人都需要注释掉。我有这个代码,主要是因为我的chrome是我用源码替换掉的。所以没写入到系统中。然后就手动写执行文件所在的地址好了。一般人是不需要这段代码的。记得注释掉!!!
下载好需要的库~
之后直接运行就好了。用时蛮久的。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import pickle
import pandas as pd
#####################
USERNAME = "写自己的手机号码"
# 填写好账号 密码 下面的url根据要求替换掉
URL = "https://www.duocaitou.com/project?projectStatus=6&pageNum=%d"
pageCount = 1
tempURL = URL % pageCount
LOGIN_URL = "https://www.duocaitou.com/login?redirect=%2Fproject"
refresh = False
#####################
# configure
options = webdriver.ChromeOptions()
options.binary_location = r"D:\Software\Chrome\Application\chrome.exe"
browser = webdriver.Chrome(chrome_options=options)
keys = ['项目公司', '项目公司估值', '项目估值', '筹集规模', '筹集模式', '起投金额', '每人限投', '投资期限']
globalData = {}
for k in keys:
globalData[k] = []
if refresh:
browser.get(LOGIN_URL)
# 登录
time.sleep(2)
input = browser.find_element_by_xpath('//input[@placeholder="请输入手机号"]')
input.send_keys(USERNAME)
time.sleep(40)
pickle.dump(browser.get_cookies(), open("cookies.pkl", "wb"))
print('finish refresh')
browser.get(tempURL) # 加载网页
time.sleep(2)
else:
browser.get(LOGIN_URL) # 加载网页
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
browser.add_cookie(cookie)
browser.get(tempURL) # 加载网页
time.sleep(2)
print(tempURL)
save = 0
times = 0
index_i = 0
continuetime = 0
try:
while True:
try:
elemnt = WebDriverWait(browser, 10).until(
EC.presence_of_element_located(
(By.XPATH, '//*[@id="__layout"]/div/div[2]/div/div[3]/ul/li[%d]/div/div/div[1]/div[2]/h3' % (
index_i + 1)))
)
li_list = browser.find_element_by_xpath(
'//*[@id="__layout"]/div/div[2]/div/div[3]/ul/li[%d]/div/div/div[1]/div[2]/h3' % (index_i + 1)).click()
elemnt = WebDriverWait(browser, 10).until(
EC.presence_of_element_located(
(By.XPATH, '//*[@id="__layout"]/div/div[2]/div/div[1]/div[1]/div[2]/div[1]/div/div/div/div/div[3]'))
)
browser.find_element_by_xpath(
'//*[@id="__layout"]/div/div[2]/div/div[1]/div[1]/div[2]/div[1]/div/div/div/div/div[3]').click()
elemnt = WebDriverWait(browser, 10).until(
EC.presence_of_element_located(
(By.XPATH, '//*[@id="__layout"]/div/div[2]/div/div[1]/div[1]/div[2]/div[2]/div[2]/div/span'))
)
datas = browser.find_elements_by_xpath(
'//*[@id="__layout"]/div/div[2]/div/div[1]/div[1]/div[2]/div[2]/div[2]/div/span')
if "筹集模式" in datas[0].text:
datas = datas[0]
else:
datas = datas[-1]
elemnt = WebDriverWait(datas, 10).until(
EC.presence_of_element_located(
(By.TAG_NAME, 'p'))
)
time.sleep(5 + continuetime)
datas = datas.find_elements_by_tag_name('p')[0:40]
except Exception as e:
browser.get(tempURL) # 加载网页
print('refresh', tempURL, 'index is', index_i)
time.sleep(5)
continuetime += 1
if continuetime >= 10:
break
continue
datas = [d for d in datas if len(d.text.strip()) > 4 and (':' in d.text or ':' in d.text)][:8]
tempDatas = []
for d in datas:
try:
td = d.find_element_by_tag_name('span').get_attribute("innerHTML")
if '
' in td:
tds = td.split('
')
for tdi in tds:
tempDatas.append(tdi.replace('\n', '').replace(' ', ' ').replace(': ', ':').replace(':', ":").strip())
else:
tempDatas.append(d.text.strip().replace('\n', '').replace(': ', ':').replace(':', ":"))
except Exception as e:
tempDatas.append(d.text.strip().replace('\n', '').replace(': ', ':').replace(':', ":"))
datas = tempDatas
try:
tempdict = {}
for d in datas:
if ':' not in d:
continue
a = d[:d.index(':')]
b = d[d.index(':') + 1:]
if a == '最高可投':
a = '每人限投'
a = a.replace("标的公司", "项目公司").replace("标的估值", "项目公司估值")
if a in tempdict:
if '筹集规模:' in b:
b = b.split('筹集规模:')
tempdict['项目公司估值'] = b[0]
tempdict['筹集规模'] = b[1]
else:
tempdict['项目公司估值'] = b
elif a == '筹集模式' and '每人限投:' in b:
b = b.split('每人限投:')
tempdict[a] = b[0]
tempdict['每人限投'] = b[1]
elif a == '项目公司' and '筹集规模:' in b:
b = b.split('筹集规模:')
tempdict['项目公司估值'] = b[0]
tempdict['筹集规模'] = b[1]
else:
tempdict[a] = b
if len(tempdict) == 0 or '项目公司' not in tempdict:
continuetime += 1
if continuetime < 3:
browser.get(tempURL) # 加载网页
time.sleep(2)
continue
else:
continuetime = 0
for key in globalData.keys():
if key in tempdict:
globalData[key].append(tempdict[key])
elif key == '标的公司':
globalData['项目公司'].append(tempdict[key])
elif key == '标的公司估值':
globalData['项目公司估值'].append(tempdict[key])
else:
globalData[key].append('NULL')
print(key, ': ', globalData[key][-1], end=' , ')
print()
times += 1
if times % 50 == 49:
pd.DataFrame(globalData).to_excel('data_%d.xlsx' % save, columns=keys)
save += 1
times = 0
except Exception as e:
print(e.args)
for i, d in enumerate(datas):
print(i, d)
break
if index_i == 8:
index_i = 0
pageCount += 1
if pageCount >= 67:
break
tempURL = URL % pageCount
print(tempURL)
browser.get(tempURL) # 加载网页
time.sleep(2)
else:
browser.get(tempURL) # 加载网页
time.sleep(2)
index_i += 1
continuetime = 0
finally:
pd.DataFrame(globalData).to_excel('data.xlsx', columns=keys)