选择器没有结果

时间:2019-03-16 06:04:09

标签: python web-scraping web-crawler

我尝试抓取电影网站。 没有错误,但没有结果 没有iframe。然后我选择选择位置 通过chrome> F12>复制>复制选择器

站点链接为CGV movie

Element what i select

import requests
from bs4 import BeautifulSoup
import json
import os

## python file location
BASE_DIR = os.path.dirname(os.path.abspath(__file__))

req = requests.get('http://ticket.cgv.co.kr/Reservation/Reservation.aspx?MOVIE_CD=&MOVIE_CD_GROUP=&PLAY_YMD=&THEATER_CD=&PLAY_NUM=&PLAY_START_TM=&AREA_CD=&SCREEN_CD=&THIRD_ITEM=')
html = req.text
soup = BeautifulSoup(html, 'html.parser')
movie_titles = soup.select(
    '#movie_list > ul > li > a > span.text'
    )

data = {}

for title in movie_titles:
    data = title.text + "\n"

with open(os.path.join(BASE_DIR, 'result.json'), 'w+') as json_file:
    json.dump(data, json_file)

1 个答案:

答案 0 :(得分:0)

您想要的数据是动态加载的。使用诸如硒之类的方法

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

url = 'http://ticket.cgv.co.kr/Reservation/Reservation.aspx?MOVIE_CD=&MOVIE_CD_GROUP=&PLAY_YMD=&THEATER_CD=&PLAY_NUM=&PLAY_START_TM=&AREA_CD=&SCREEN_CD=&THIRD_ITEM='
driver = webdriver.Chrome()
driver.get(url)
data = [item.text for item in WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "#movie_list > ul > li > a > span.text")))]
print(data)

您可以将CSS选择器缩短到

#movie_list .text