会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132358个问题

scrapy03.zip

老师,我写的只能爬取到第一页的数据,第二页的数据就显示读取不到了

c5c9f554431fa6dbfb1a79d583d98e5.png

Python 全系列/第十五阶段:Python 爬虫开发/scrapy框架使用(旧) 421楼
Python 全系列/第十五阶段:Python 爬虫开发/scrapy框架使用(旧) 423楼
Python 全系列/第十五阶段:Python 爬虫开发/移动端爬虫开发- 424楼
Python 全系列/第十五阶段:Python 爬虫开发/移动端爬虫开发- 425楼
Python 全系列/第十五阶段:Python 爬虫开发/移动端爬虫开发- 426楼
Python 全系列/第十五阶段:Python 爬虫开发/爬虫反反爬- 427楼
Python 全系列/第十五阶段:Python 爬虫开发/爬虫反反爬- 428楼
Python 全系列/第十五阶段:Python 爬虫开发/爬虫反反爬- 429楼
Python 全系列/第十五阶段:Python 爬虫开发/scrapy框架使用(旧) 432楼

之前用request爬取猫眼电影时,

由于猫眼把电影信息变成了动态获取,因此无法直接用源代码爬取。

现在通过selenium直接点进电影信息时发现,

即使首页面加了防检测,但只能在当前页面生效,打开的新页面window.navigator.wevdrive===True,依旧无法获取电影信息,因此仍无法用selenium获取多条电影信息。

因此通过request爬取电影目录,selenium爬取电影信息,总算是成功爬取了电影信息,目前猫眼评分转了码问题仍未解决

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from time import sleep
import requests
from fake_useragent import UserAgent
from lxml import etree
from queue import Queue
from threading import Thread


def get_film_list(page):
    film_list = []
    for i in range(page):
        url = f'https://www.maoyan.com/films/?showType=3&offset={30*i}'
        headers = {'User-Agent': UserAgent().chrome}
        resp = requests.get(url, headers = headers)
        html = etree.HTML(resp.text)
        films = html.xpath('//dd/div[@title]/a/@href')
        for film in films:
            film_list.append(film)
        sleep(1)
    return film_list


def get_film_data(film):
    url = f'https://www.maoyan.com{film}'
    options = webdriver.ChromeOptions()
    # 设置无头
    options.add_argument('--headless')
    # 防检测1
    options.add_experimental_option('excludeSwitches', ['enable-automation'])
    options.add_experimental_option('useAutomationExtension', False)

    service = Service(executable_path='./tools/chromedriver')
    chrome = webdriver.Chrome(service=service, options=options)
    chrome.implicitly_wait(2)

    # 防检测2
    chrome.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
        "source": """
            Object.defineProperty(navigator, 'webdriver', {
              get: () => false
            })
          """
    })

    chrome.get(url)
    name = chrome.find_element(By.XPATH, '//h1[@class="name"]').text
    type_ = [i.text for i in chrome.find_elements(By.XPATH, '//a[@class="text-link"]')]
    chrome.find_element(By.XPATH, '//div[@class="tab-title "]').click()
    actors = []
    for i in chrome.find_elements(By.XPATH, '//li[@class="celebrity actor"]/div/a'):
        if i.text and i.text not in actors:
            actors.append(i.text.strip())

    info = {'电影名': name, '类型': type_, '主演': actors}
    return info


def create_quere(list):
    q = Queue()
    for i in list:
        q.put(i)
    return q


class MyThread(Thread):
    def __init__(self, q):
        Thread.__init__(self)
        self.__q = q

    def run(self) -> None:
        while not self.__q.empty():
            film = self.__q.get()
            film_info = get_film_data(film)
            print(film_info)


if __name__ == '__main__':
    film_list = get_film_list(1)
    q = create_quere(film_list)
    for i in range(3):
        t = MyThread(q)
        t.start()


Python 全系列/第十五阶段:Python 爬虫开发/爬虫基础(旧) 433楼
Python 全系列/第十五阶段:Python 爬虫开发/爬虫基础(旧) 434楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637