加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
iwencai.py 2.64 KB
一键复制 编辑 原始数据 按行查看 历史
chenxiaoyu 提交于 2022-04-23 20:02 . gdhs
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
class SearchWenCai:
def parse_value(self):
result = self.br.find_elements_by_css_selector(".iwc-table-body >table >tbody >tr >td >div")
tableData = []
column_count = len(result) // 100
if '/' in result[6].text: # 结果不包含现价那一列
for i in range(100):
rowData = [result[column_count * i + 0].text, result[column_count * i + 2].text,
result[column_count * i + 3].text, result[column_count * i + 5].text, # 6
result[column_count * i + 8].text, result[column_count * i + 9].text] # 9 10
tableData.append(rowData)
else:
for i in range(100):
rowData = [result[column_count * i + 0].text, result[column_count * i + 2].text,
result[column_count * i + 3].text, result[column_count * i + 6].text, # 6
result[column_count * i + 9].text, result[column_count * i + 10].text] # 9 10
tableData.append(rowData)
return tableData
def stealth(self):
# execute stealth.min.js
with open('stealth/stealth.min.js') as f:
js = f.read()
self.br.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": js
})
def search(self, keyValue):
# 打开页面搜索
self.br.get(
'http://www.iwencai.com/unifiedwap/result?w={}&querytype=stock'.format(keyValue))
time.sleep(1)
if (self.first_search):
# 翻页
page = self.br.find_element_by_css_selector(".pcwencai-pagination-wrap>.drop-down-box>.drop-down-arrow")
page.click()
time.sleep(1)
checktext = self.br.find_elements_by_css_selector(".pcwencai-pagination-wrap>.drop-down-box>div>ul>li")
checktext[-1].click()
time.sleep(5)
self.first_search = False
start = time.time()
data = self.parse_value()
print("解析耗时:{} 秒".format(time.time() - start))
return data
def close(self):
self.br.close()
def __init__(self, headless=True):
chrome_options = Options()
if (headless):
chrome_options.add_argument("--headless")
chrome_options.add_argument(
'user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4240.198 Safari/537.36')
self.br = webdriver.Chrome(options=chrome_options)
self.stealth()
self.first_search = True
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化