会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132647个问题
Python 全系列/第十六阶段:Python 爬虫开发/爬虫反反爬- 61楼
Python 全系列/第十六阶段:Python 爬虫开发/scrapy 框架高级 62楼
Python 全系列/第十六阶段:Python 爬虫开发/爬虫基础 64楼
Python 全系列/第十六阶段:Python 爬虫开发/爬虫反反爬 65楼
Python 全系列/第十六阶段:Python 爬虫开发/scrapy 框架高级 67楼
Python 全系列/第十六阶段:Python 爬虫开发/移动端爬虫开发- 68楼

# _*_coding=utf-8 _*_
from time import sleep

import requests
from fake_useragent import UserAgent
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
from huadong import distance, track


def save_img():
    url = 'https://www.sf-express.com/cn/sc/dynamic_function/waybill/#search/bill-number/SF1406050054883'
    chrome = webdriver.Chrome()
    chrome.get(url)
    wait = WebDriverWait(chrome, 5)

    try:
        wait.until(ec.presence_of_element_located((By.ID, 'tcaptcha_popup')))
        # 切换窗口
        chrome.switch_to_frame('tcaptcha_popup')
        # 获取图片
        img = chrome.find_element_by_id('slideBkg')
        img_src = img.get_attribute('src')[:-1]
        # 下载图片
        download_img(img_src + '1', 'ctp1.png')
        download_img(img_src + '2', 'ctp2.png')
        # 获取滑动的距离
        tmp_distance = distance.get_long()
        # 生成滑动轨迹
        tk = track.get_track(tmp_distance-12)
        # 滑动按钮
        # 选中按钮
        button = chrome.find_element_by_id('tcaptcha_drag_button')
        webdriver.ActionChains(chrome).click_and_hold(button).perform()
        # 按轨迹滑动按钮
        for t in tk:
            webdriver.ActionChains(chrome).move_by_offset(xoffset=t,yoffset=0).perform()
        # 释放按钮
        webdriver.ActionChains(chrome).release().perform()
        # 休眠2秒
        sleep(2)
        chrome.quit()
    except Exception as e:
        print(e)
        chrome.quit()


def download_img(url, filename):
    # print(url)
    headers = {
        'User-Agent': UserAgent().chrome
    }
    resp = requests.get(url, headers=headers)

    with open(f'./imgs/{filename}', 'wb') as f:
        f.write(resp.content)


if __name__ == '__main__':
    save_img()

image.png

老师为啥报这个错误,,路径啥的都对啊

Python 全系列/第十六阶段:Python 爬虫开发/爬虫反反爬- 69楼
Python 全系列/第十六阶段:Python 爬虫开发/scrapy框架使用(旧) 70楼
Python 全系列/第十六阶段:Python 爬虫开发/移动端爬虫 71楼

scrapy03.zip

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

c5c9f554431fa6dbfb1a79d583d98e5.png

Python 全系列/第十六阶段:Python 爬虫开发/scrapy框架使用(旧) 73楼
Python 全系列/第十六阶段:Python 爬虫开发/scrapy 框架高级 74楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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