会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132647个问题
Python 全系列/第十六阶段:Python 爬虫开发/scrapy框架使用(旧) 766楼

老师麻烦帮我看下代码,我的ip好像是不是被封了?响应回来的html好像是要我验证

import requests
from fake_useragent import UserAgent
from lxml import etree

def get_html(url):
    '''传入要爬取的地址,返回html'''
    headers = {"User-Agent": UserAgent().chrome}
    response = requests.get(url,headers)
    if response.status_code == 200:
        response.encoding = 'utf-8'
        print(response.text)
        return response.text
    else:
        return None         

def parse_list(html):
    '''传入含有电影信息的html,返回电影列表的每个电影的html'''
    e = etree.HTML(html)
    list_url = ['https://maoyan.com/{}'.format(url) for url in e.xpath('//div[@class="movie-item film-channel"]/a/@href')]
    # print(list_url)
    return list_url

def parse_index(html):
    '''传入有电影信息的html,返回提取到的电影信息'''
    e = etree.HTML(html)
    name = e.xpath('//h1[@class="name"]/text()') 
    type = e.xpath('//li[@class="ellipsis"][1]/a/text()')
    actor = e.xpath('//div[@class="celebrity-group"][2]/ul[@class="celebrity-list clearfix"]/li/div/a/text()')
    actors = format_data(actor)
    return {'name':name,'type':type,'actor':actors}

def format_data(actors):
    actor_set = set()
    for actor in actors:
        actor_set.add(actor)
    return actor_set

def main():
    num = int(input('请输入要获取的页数:'))
    for page in range(num):
        url = 'https://maoyan.com/films?showType=3&offset={}'.format(page*30)
        list_html = get_html(url)
        list_url = parse_list(list_html)
        print(list_url)
        # for url in list_url:
        #     info_html = get_html(url)
        #     movie = parse_index(info_html)
        #     print(movie)


if __name__ == "__main__":
    main()

image.png

Python 全系列/第十六阶段:Python 爬虫开发/爬虫反反爬- 768楼
Python 全系列/第十六阶段:Python 爬虫开发/爬虫基础(旧) 769楼

老师就是网页没有爬取完全,程序就结束了,然后我加了隐式等待和调用js,但是网页还是没有爬取完全,是不是我的逻辑方面有错误,爬取时,还会有那个提示弹窗,必须手动点掉才开始爬取,怎么在代码里就给他跳过

from selenium.webdriver.chrome.service import Service

from selenium.webdriver import Chrome

from lxml import etree

from selenium.webdriver.common.by import By

from time import sleep


def spider_huya():

    #创建驱动

    service=Service('./chromedriver.exe')

    #创建浏览器

    driver=Chrome(service=service)

    #访问网页

    driver.get('https://www.huya.com/g/lol')

    count=1

    while True:

        print('获取了第{%d}页' % count)

        count +=1

        #拖动滚动条到底部

        js='document.documentElement.scrollTop=1000'

        driver.execute_script(js)

         #设置隐式等待

        driver.implicitly_wait(20)

        #创建etree对象

        e=etree.HTML(driver.page_source)

        #提取数据

        names=e.xpath('//i[@class="nick"]/text()')

        persons=e.xpath('//i[@class="js-num"]/text()')

        sleep(3)

       

        #打印数据

        #for n,p in zip(names,persons):

             #print(f'主播名:{n} 人气:{p}')

        #获取选项卡,点击

        if driver.page_source.find('laypage_next') ==-1:

            break

        next_btn=driver.find_element(By.XPATH,'//a[@class="laypage_next"]')

        next_btn.click()

        sleep(3)

    driver.quit()

 

if  __name__=='__main__':

    spider_huya()

594d0d95d70c3aaa787599017a3c0d3.png

Python 全系列/第十六阶段:Python 爬虫开发/爬虫基础 770楼
Python 全系列/第十六阶段:Python 爬虫开发/scrapy框架使用 771楼
Python 全系列/第十六阶段:Python 爬虫开发/爬虫反反爬 772楼
Python 全系列/第十六阶段:Python 爬虫开发/爬虫基础(旧) 773楼
Python 全系列/第十六阶段:Python 爬虫开发/爬虫反反爬 774楼

game.zip

老师 这个文件不生成 txt文件 是什么原因?

Python 全系列/第十六阶段:Python 爬虫开发/移动端爬虫开发- 775楼
Python 全系列/第十六阶段:Python 爬虫开发/爬虫反反爬- 776楼
Python 全系列/第十六阶段:Python 爬虫开发/爬虫反反爬 777楼
Python 全系列/第十六阶段:Python 爬虫开发/scrapy框架使用 778楼
Python 全系列/第十六阶段:Python 爬虫开发/爬虫基础(旧) 779楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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