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

import scrapy
import re
from chaojiying_Python.chaojiying import get_code

class Login1Spider(scrapy.Spider):
    name = 'login1'
    allowed_domains = ['ganji.com']
    start_urls = ['https://passport.ganji.com/login.php']
    def parse(self, response):
        img_url = 'https://passport.ganji.com/ajax.php?dir=captcha&module=login_captcha'
        hash_code =re.search(r'"__hash__":"(.+)"',response.text).group(1)
        yield scrapy.Request(img_url,callback=self.do_fromdata,meta={'hash_code':hash_code})
    def do_fromdata(self,response):
        with open('code.jpg','wb') as f:
            f.write(response.body)
        #code = get_code('code.jpg')
        code = input("请输入验证码:")
        hash_code = response.request.meta['hash_code']
        data = {
            'username': '17030240219',
            'password': '123456qaz',
            'setcookie': '14',
            'checkCode':code,
            'next': '/ user / register_success.php?username=17030240219&next=%2F',
            'source':'passport',
            '__hash__':hash_code
        }
        login_url = 'https://passport.ganji.com/login.php'
        yield scrapy.FormRequest(login_url,method='POST',formdata=data,callback=self.after_login)
        #print(response.text)
    def after_login(self,response):
        print(response.text)

为什么登录时显示无效数组长度,这个怎么弄

Python 全系列/第十五阶段:Python 爬虫开发/scrapy 框架高级 797楼

代码:

import requests
from fake_useragent import UserAgent
from lxml import etree
from time import sleep
url="https://www.lagou.com/zhaopin/2/?filterOption=3&sid=021dfdfa56f24b11810b1f18e04902eb"
headers={"User-Agent":UserAgent().chrome}
sleep(2)
resp=requests.get(url,headers=headers)
resp.encoding="utf-8"
html=resp.text
print(html)
e=etree.HTML(html)
a=e.xpath('//div[@class="p_top"]//h3/text()')
for i in a:
    print(i)

运行结果:


        body {

            margin: 0;

            width: 100%;

            height: 100%;

        }


        @keyframes loadingAnimation {

            0% {

                transform: translate3d(0, 0, 0);

            }


            50% {

                transform: translate3d(0, -10px, 0);

            }

        }


        .loading-info {

            text-align: center;

            height: 100%;

            position: relative;

            background: #fff;

            top: 50%;

            margin-top: -37px;

        }


        .loading-info .animation-word {

            width: 100%;

        }


        .loading-info .animation-word p {

            margin-top: 10px;

            color: #9fa3b0;

        }


        .animation-word .component-l,

        .animation-word .component-a,

        .animation-word .component-g,

        .animation-word .component-o,

        .animation-word .component-u {

            display: inline-block;

            width: 40px;

            height: 42px;

            line-height: 42px;

            font-family: Helvetica Neue, Helvetica, Arial, Hiragino Sans GB, Hiragino Sans GB W3, Microsoft YaHei UI, Microsoft YaHei, WenQuanYi Micro Hei, sans-serif;

            font-weight: bolder;

            font-size: 40px;

            color: #eceef2;

            vertical-align: top;

            -webkit-animation-fill-mode: both;

            animation-fill-mode: both;

            -webkit-animation: loadingAnimation 0.6s infinite linear alternate;

            -moz-animation: loadingAnimation 0.6s infinite linear alternate;

            animation: loadingAnimation 0.6s infinite linear alternate;

        }


        .animation-word .component-a {

            -webkit-animation-delay: 0.1s;

            -moz-animation-delay: 0.1s;

            animation-delay: 0.1s;

        }


        .animation-word .component-g {

            -webkit-animation-delay: 0.2s;

            -moz-animation-delay: 0.2s;

            animation-delay: 0.2s;

        }


        .animation-word .component-o {

            -webkit-animation-delay: 0.3s;

            -moz-animation-delay: 0.3s;

            animation-delay: 0.3s;

        }


        .animation-word .component-u {

            -webkit-animation-delay: 0.4s;

            -moz-animation-delay: 0.4s;

            animation-delay: 0.4s;

        }

    </style>

</head>


<body>

    <div class="loading-info">

        <div class="info-inner">

            <div class="animation-word">

                <span class="component-l">L</span>

                <span class="component-a">a</span>

                <span class="component-g">g</span>

                <span class="component-o">o</span>

                <span class="component-u">u</span>

                <p class="gray">正在加载中...</p>

            </div>

        </div>

    </div>

    <script>

        var securityPageName = "securityCheck";

        !function () {

            function e(c) {

                var l, m, n, o, p, q, r, e = function () {

                    var a = location.hostname;

                    return "localhost" === a || /^(\d+\.){3}\d+$/.test(a) ? a : "." + a.split(".").slice(-2).join(".")

                }(),

                    f = function (a, b) {

                        var f = document.createElement("script");

                        f.setAttribute("type", "text/javascript"), f.setAttribute("charset", "UTF-8"), f.onload = f.onreadystatechange = function () {

                            d && "loaded" != this.readyState && "complete" != this.readyState || b()

                        }, f.setAttribute("src", a), "IFRAME" != c.tagName ? c.appendChild(f) : c.contentDocument ? c.contentDocument.body ? c.contentDocument.body.appendChild(f) : c.contentDocument.documentElement.appendChild(f) : c.document && (c.document.body ? c.document.body.appendChild(f) : c.document.documentElement.appendChild(f))

                    },

                    g = function (a) {

                        var b = new RegExp("(^|&)" + a + "=([^&]*)(&|$)"),

                            c = window.location.search.substr(1).match(b);

                        return null != c ? unescape(c[2]) : null

                    },

                    h = {

                        get: function (a) {

                            var b, c = new RegExp("(^| )" + a + "=([^;]*)(;|$)");

                            return (b = document.cookie.match(c)) ? unescape(b[2]) : null

                        },

                        set: function (a, b, c, d, e) {

                            var g, f = a + "=" + encodeURIComponent(b);

                            c && (g = new Date(c).toGMTString(), f += ";expires=" + g), f = d ? f + ";domain=" + d : f, f = e ? f + ";path=" + e : f, document.cookie = f

                        }

                    },

                    i = function (a) {

                        if (a) {

                            window.location.replace(a)

                        }

                    },

                    j = function (a, c) {

                        c || a.indexOf("security-check.html") > -1 ? i(c) : i(a);

                    };

                window.location.href, l = g("seed") || "", m = g("ts"), n = g("name"),

                    o = g("callbackUrl"),

                    p = g("srcReferer") || "", "null" !== n && l && n && o, l && m && n && (f("dist/" + n + ".js", function () {

                        var n, a = (new Date).getTime() + 1728e5,

                            d = "",

                            f = {},

                            g = window.gt || c.contentWindow.gt;

                        try {

                            d = (new g()).a();

                        } catch (k) { console.log(k) }

                        if (d) {

                            (h.set("__lg_stoken__", d, a, e, "/"), j(p, o))

                        }

                    }))

            }

            function j(a) {

                if (!f && !g && document.addEventListener) return document.addEventListener("DOMContentLoaded", a, !1);

                if (!(h.push(a) > 1))

                    if (f) ! function () {

                        try {

                            document.documentElement.doScroll("left"), i()

                        } catch (a) {

                            setTimeout(arguments.callee, 0)

                        }

                    }();

                    else if (g) var b = setInterval(function () {

                        /^(loaded|complete)$/.test(document.readyState) && (clearInterval(b), i())

                    }, 0)

            }

            var d, f, g, h, i, a = 0,

                b = (new Date).getTime(),

                c = window.navigator.userAgent;

            c.indexOf("MSIE ") > -1 && (d = !0),

                f = !(!window.attachEvent || window.opera),

                g = /webkit\/(\d+)/i.test(navigator.userAgent) && RegExp.$1 < 525,

                h = [],

                i = function () {

                    for (var a = 0; a < h.length; a++) h[a]()

                };

            j(function () {

                var b, a = window.navigator.userAgent.toLowerCase();

                return "micromessenger" !== a.match(/micromessenger/i) || "wkwebview" == a.match(/wkwebview/i) ?

                    (e(document.getElementsByTagName("head").item(0)), void 0)

                    :

                    (b = document.createElement("iframe"), b.style.height = 0, b.style.width = 0, b.style.margin = 0, b.style.padding = 0, b.style.border = "0 none", b.name = "tokenframe", b.src = "about:blank", b.attachEvent ? b.attachEvent("onload", function () {

                        e(b)

                    }) : b.onload = function () {

                        e(b)

                    }, (document.body || document.documentElement).appendChild(b), void 0)

            })

        }();


    </script>

<script type="text/javascript" crossorigin="anonymous" src="https://www.lagou.com/upload/oss.js?v=1010"></script></body>


</html>

老师请问一下,为什么我使用requests.get方法爬取拉勾网的时候,返回回来的HTML代码和使用f12查看的源代码不一致?对于这个问题当我使用selenium库爬取的时候就不会出现,请问老师在爬取拉勾网的时候是不是必须使用selenium库进行爬取呢?如果我想用requests.get方法来爬取拉勾网,有没有什么办法可以解决返回的HTML和源码不一致的问题?

Python 全系列/第十五阶段:Python 爬虫开发/爬虫反反爬- 803楼

代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
options=webdriver.ChromeOptions()
options.add_argument('--headless')
chrome=webdriver.Chrome(options=options)
url="https://www.lagou.com/zhaopin/Python/?filterOption=3&sid=9146e755a7bf4f6dac59edd3e7d43127"
sleep(2)
chrome.get(url)
html=chrome.page_source
print(html)
while True:
    job = chrome.find_elements(By.XPATH, '//div[@class="p_top"]//h3')
    point = chrome.find_elements(By.XPATH, '//span[@class="add"]//em')
    salary = chrome.find_elements(By.XPATH, '//div[@class="li_b_l"]//span[@class="money"]')
    company = chrome.find_elements(By.XPATH, '//div[@class="company_name"]/a')
    industry = chrome.find_elements(By.XPATH, '//div[@class="industry"]')
    n = len(job)
    with open(r"job5.txt", "a", encoding="utf-8") as f:
        for i in range(n):
            f.write((job[i].text).strip())
            f.write("\t")
            f.write((point[i].text).strip())
            f.write("\t")
            f.write((salary[i].text).strip())
            f.write("\t")
            f.write((company[i].text).strip())
            f.write("\t")
            f.write((industry[i].text).strip())
            f.write("\n")
    if html.find('page_no') != -1:
        chrome.find_element_by_class_name('page_no').click()
        sleep(3)
    else:
        break
chrome.quit()

运行结果:

高级爬虫工程师()  杭州滨江区 丁香园    医疗丨健康企业服务 轮及以上 人
开发工程师    上海长宁区 信飞数科信用飞首付游   科技金融 轮 人
开发工程师    上海徐汇区 问卷网众言科技   工具类产品 轮 人
开发   上海徐汇区 问卷网众言科技   工具类产品 轮 人
开发工程师    北京知春路 天眼查    数据服务|咨询 不需要融资 人
开发工程师    东莞东莞市东莞美信   汽车丨出行 上市公司 人以上
开发工程师    杭州余杭区 和骏出行   移动互联网 天使轮 人
开发工程师    上海徐汇区 上海江煦信息科技   软件服务|咨询 不需要融资 人
开发工程师    杭州西湖区 涂鸦智能   物联网智能硬件 轮 人以上
高级开发工程师  上海长宁区 信飞数科信用飞首付游   科技金融 轮 人
开发工程师    上海张江  仑动科技   企业服务人工智能 未融资 少于人
开发工程师    广州白云区 东莞美信   汽车丨出行 上市公司 人以上
开发工程师    深圳前海  易博天下   软件服务|咨询 上市公司 人
开发工程师    广州海珠区 派客朴食   移动互联网电商 不需要融资 人
上海浦东新北纬三十度  电商平台 未融资 人
高级爬虫工程师() 杭州滨江区 丁香园    医疗丨健康企业服务 轮及以上 人
开发工程师    上海长宁区 信飞数科信用飞首付游   科技金融 轮 人
开发工程师    上海徐汇区 问卷网众言科技   工具类产品 轮 人
开发   上海徐汇区 问卷网众言科技   工具类产品 轮 人
开发工程师    北京知春路 天眼查    数据服务|咨询 不需要融资 人
开发工程师    东莞东莞市东莞美信   汽车丨出行 上市公司 人以上
开发工程师    杭州余杭区 和骏出行   移动互联网 天使轮 人
开发工程师    上海徐汇区 上海江煦信息科技   软件服务|咨询 不需要融资 人
开发工程师    杭州西湖区 涂鸦智能   物联网智能硬件 轮 人以上
高级开发工程师  上海长宁区 信飞数科信用飞首付游   科技金融 轮 人
开发工程师    上海张江  仑动科技   企业服务人工智能 未融资 少于人
开发工程师    广州白云区 东莞美信   汽车丨出行 上市公司 人以上
开发工程师    深圳前海  易博天下   软件服务|咨询 上市公司 人
开发工程师    广州海珠区 派客朴食   移动互联网电商 不需要融资 人
上海浦东新北纬三十度  电商平台 未融资 人

问题:

老师请问一下,我使用的是selenium来爬取拉勾网上的关于python的职位,第一页中的数据是从高级python爬虫工程师(Insight)15k-25k到python上海.浦东新区15k-30k,当我爬取完一页以后我就用chrome中的click方法点击进入下一页继续爬取,我使用的是while True循环来实现的,可是为什么当我点击进入下一页以后爬取的还是第一页的15条数据,每次都是在重复爬取第一页的15条数据,麻烦老师帮我看看我的代码哪里出错了?

Python 全系列/第十五阶段:Python 爬虫开发/爬虫反反爬- 804楼

代码:

mport requests
from bs4 import BeautifulSoup
from fake_useragent import UserAgent
from time import sleep
def get_url(url):
    proxies={"http":"http://61.135.155.82:443"}
    headers={"User-Agent":UserAgent().random}
    # sleep(2)
    resp=requests.get(url,headers=headers,proxies=proxies,timeout=5)
    resp.encoding="utf-8"
    if resp.status_code==200:
        return resp.text
    else:
        return None
def parse_list(html):
    soup=BeautifulSoup(html,'lxml')
    movie_list=["http://maoyan.com{}".format(a.get('href')) for a in soup.select('dl[class="movie-list"] dd>div[class="movie-item film-channel"]>a')]
    return movie_list
def parse_index(html):
    soup=BeautifulSoup(html,'lxml')
    title=soup.select('div[class="movie-brief-container"]>h1')
    # type=soup.select('div[class="movie-brief-container"]>ul>li>a')
    print(title[0].text)
    # print(type[0].text)
def main():
    url="https://maoyan.com/films?showType=3&offset=0"
    html=get_url(url)
    movie_list=parse_list(html)
    for url in movie_list:
        # print(url)
        html=get_url(url)
        parse_index(html)
if __name__=="__main__":
    main()

运行结果:

屏幕截图 2021-03-11 073945.png

老师请问一下,为什么我的程序timeout错误,网上说要设置一个timeout时间,我设置了一个timeout=5可是还是报错,请问这是什么原因?麻烦老师帮我看一下

Python 全系列/第十五阶段:Python 爬虫开发/爬虫反反爬- 808楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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