问财同花顺spider(爬虫)2接上篇没有实现的页面滚动问题

上一篇的文章

看过我之前文章的可以知道无法实现鼠标滚动问题

这里滚动页面运用到查找元素拖动到元素位置,由于更新数据按照30个更新,使用设置好参数就行。
话说之前那个手动滚动的想法很二,我顶,代码可以实现为什么要手动呢?

target = browser.find_element_by_xpath("/html/body/div[1]/div[1]/div/div[1]/div[1]/div/div[1]/div[2]/div/div/div/div/div[2]/div[2]/div/div[2]/table/tr["+str(i)+"]/td[1]/span")
  browser.execute_script("arguments[0].scrollIntoView();", target) 

话不多说,上代码,逆向js是自己写的,需要的话和我交流(1440414483@qq)

网速不太好,使用设置了很大wait机制,wait呢要比time.sleep好多了,time.sleep我还得浪费时间去设置,网速好的时候就好了,也怕报错
当然我这里的页数页数我爬取的网页的数据量,后面有待改进

上代码

import time
import os
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
import json
import requests
from requests.exceptions import RequestException
import re
from bs4 import BeautifulSoup
import csv
from tqdm import tqdm
import pandas as pd
import os
os.getcwd() #获取当前工作路径

chrome_options = webdriver.ChromeOptions()
with open('xx需要请找我.js') as f:
    js = f.read()
time.sleep(2) 
chrome_options.add_argument('user-agent="Mozilla/5.0 (iPod; U; CPU iPhone OS 2_1 like Mac OS X; ja-jp) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5F137 Safari/525.20"')
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
browser = webdriver.Chrome(options = chrome_options)
browser.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
  "xxxx": js
})
browser.get('http://www.iwencai/unifiedwap/result?w=5g&querytype=&issugs')
WebDriverWait(browser,40,0.2).until(lambda x:x.find_element_by_xpath("/html/body/div[1]/div[1]/div/div[1]/div[1]/div/div[1]/div[2]/div/div/div/div/div[2]/div[2]/div/div[3]"))
browser.find_element_by_xpath('/html/body/div[1]/div[1]/div/div[1]/div[1]/div/div[1]/div[2]/div/div/div/div/div[2]/div[2]/div/div[3]').click()

#拖动到可见的元素去
i=0
while i<(262//30)*30:
  i=i+30
  WebDriverWait(browser,300,0.2).until(lambda x:x.find_element_by_xpath("/html/body/div[1]/div[1]/div/div[1]/div[1]/div/div[1]/div[2]/div/div/div/div/div[2]/div[2]/div/div[2]/table/tr["+str(i)+"]/td[1]/span"))
  target = browser.find_element_by_xpath("/html/body/div[1]/div[1]/div/div[1]/div[1]/div/div[1]/div[2]/div/div/div/div/div[2]/div[2]/div/div[2]/table/tr["+str(i)+"]/td[1]/span")
  browser.execute_script("arguments[0].scrollIntoView();", target) 

WebDriverWait(browser,300,0.2).until(lambda x:x.find_element_by_xpath("/html/body/div[1]/div[1]/div/div[1]/div[1]/div/div[1]/div[2]/div/div/div/div/div[2]/div[2]/div/div[5]"))
target = browser.find_element_by_xpath("/html/body/div[1]/div[1]/div/div[1]/div[1]/div/div[1]/div[2]/div/div/div/div/div[2]/div[2]/div/div[5]")
browser.execute_script("arguments[0].scrollIntoView();", target) 

datas = []
for j in range(1,264):
  
  insert = (browser.find_element_by_xpath("/html/body/div[1]/div[1]/div/div[1]/div[1]/div/div[1]/div[2]/div/div/div/div/div[2]/div[2]/div/div[2]/table/tr["+str(j)+"]").text+browser.find_element_by_xpath("/html/body/div[1]/div[1]/div/div[1]/div[1]/div/div[1]/div[2]/div/div/div/div/div[2]/div[2]/div/div[1]/table/tr["+str(j)+"]").text).split('\n')
  
  datas.append(insert)
df=pd.DataFrame(datas)
df.to_csv('5g.csv',index=False,header=False)

更多推荐

问财同花顺spider(爬虫)2接上篇没有实现的页面滚动问题