【发布时间】:2020-01-22 08:06:05
【问题描述】:
在我的脚本中,我想从 instagram 搜索中获取数据,在我的代码中,selenium 按我应用的条件滚动搜索结果。但是一旦加载完成并且我尝试在我的 bs4 对象中获取滚动数据,它只返回第一个结果(60-70 之间)。它没有得到滚动后加载的数据
import time
import re
import json
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chop = Options()
chop.add_argument("--disable-gpu")
chop.add_argument("--no-sandbox")
chop.add_argument("--disable-dev-shm-usage")
driver = webdriver.Chrome(executable_path="c:/Users/Haseeb-Ahmad/3D
Objects/chromedriver.exe",options=chop)
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get('https://www.instagram.com/accounts/login/')
emailInput = driver.find_elements_by_css_selector('form input')[0]
passwordInput = driver.find_elements_by_css_selector('form input')[1]
emailInput.send_keys('#username')
passwordInput.send_keys('#password')
passwordInput.send_keys(Keys.ENTER) #login so i can avoid the login-popup
time.sleep(5)
try:
SCROLL_PAUSE_TIME = 1
driver.get("https://www.instagram.com/explore/tags/pakistan/")
x = True
count = 0
while x == True :
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(SCROLL_PAUSE_TIME)
count = count + 1
if count > 10:
x = False
else:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(SCROLL_PAUSE_TIME)
continue
bsObj = BeautifulSoup(driver.page_source,features='html.parser')
scripts = bsObj.find_all('script', type="text/javascript", text=re.compile('window._sharedData'))
stringified_json = scripts[0].get_text().replace('window._sharedData = ', '')[:-1]
data = json.loads(stringified_json)['entry_data']['TagPage'][0]['graphql'['hashtag'['edge_hashtag_to_media']['edges']
print(len(data)) #to chk data returned
finally:
driver.quit()
【问题讨论】:
标签: python selenium web-scraping beautifulsoup instagram