Python不说明code线的一个错误是指 [英] Python does not state the line of code that an error refers to

查看:451
本文介绍了Python不说明code线的一个错误是指的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图更正以下错误,基本上是关于赞成用户名和目录。我已经尝试了所有可能的方式,但没有运气。 Python不说明code线,下面的错误指的是:

 用法:Google_Map.py [-h] [-n NUM_TO_DOWNLOAD] [-l LOG_LEVEL]
                     用户名目录
Google_Map.py:错误:以下参数是必需的:用户名,目录

请在这里看到的code:

 高清__init __(个体经营,用户名,目录num_to_download = 10,
             LOG_LEVEL ='信息'):
    self.username =用户名
    self.profile_url = self.get_url(用户名)
    self.directory =目录
    self.PAUSE = 1
    self.user_agent ='的Mozilla / 4.0(兼容; MSIE 5.5; Windows NT的)'
    self.headers = {'的User-Agent:self.user_agent}
    self.html_source =无
    self.log_level = GETATTR(日志,log_level.upper())
    self.setup_logging(self.log_level)
    self.set_num_posts(num_to_download)
    self.setup_webdriver()高清GET_URL(个体经营,路径):
    返回urlparse.urljoin('https://instagram.com,路径)高清set_num_posts(个体经营,num_to_download =无):
    self.num_posts = INT(self.get_posts_count(self.profile_url)或0)
    self.num_to_download = num_to_download高清setup_logging(个体经营,级别= logging.INFO):
    self.logger = logging.getLogger('instaraider')
    self.logger.addHandler(logging.StreamHandler())
    self.logger.setLevel(logging.INFO)高清日志(个体经营,*字符串,** kwargs):
    水平= kwargs.pop('级别',logging.INFO)
    self.logger.log(水平,U''。加入(STR(S)对于s的字符串))高清setup_webdriver(个体经营):
    self.profile = webdriver.FirefoxProfile()
    self.profile.set_ preference(general.useragent.override,self.user_agent)
    self.webdriver = webdriver.Firefox(self.profile)
    self.webdriver.set_window_size(480,320)
    self.webdriver.set_window_position(800,0)高清get_posts_count(自我,网址):
    
    由于的URL Instagram的个人主页上,贴出照片数量回报
    
    响应= requests.get(URL)
    counts_ code = re.search(R'\\传媒:{计数:\\ D +',response.text)
    如果不是counts_ code:
        返回None
    返回通过re.findall(R'\\ D +',counts_ code.group())[0]高清log_in_user(个体经营):
    司机= self.webdriver
    self.log(您需要登录访问此文件。,
             你重定向到登录页面在浏览器中。,
             级别= logging.WARN)
    driver.get(self.get_url(账户/登录/'))    #等待,直到用户成功登录并redirceted
    #他/她的饲料。
    WebDriverWait(驱动程序,60)。直到(
        expected_conditions。presence_of_element_located(
            (By.CSS_SELECTOR,'-CX-私人FeedPage__feed'),
        )
    )    self.log(用户登录成功。',级别= logging.INFO)
    self.set_num_posts()#必须重新设置该
    driver.get(self.profile_url)高清load_instagram(个体经营):
    
    使用Selenium的webdriver,Instagram的加载页面获取页面的源代码
    
    self.log(self.username,'有',self.num_posts,Instagram上的帖子。)
    如果self.num_to_download不是无:
        self.log(第一,self.num_to_download的他们将被下载。)    num_to_download = self.num_to_download或self.num_posts
    司机= self.webdriver
    #负载的Instagram的个人资料,并等待暂停
    self.log(加载Instagram的个人资料......)
    driver.get(self.profile_url)
    driver.implicitly_wait(self.PAUSE)    driver.execute_script(window.scrollTo(0,document.body.scrollHeight);)
    尝试:
        EL = driver.find_element_by_css_selector(
            '-CX-私人ProfilePage__advisoryMessageHeader
        )
    除了NoSuchElementException异常:
        通过
    其他:
        self.log_in_user()    如果(num_to_download> 24):
        scroll_to_bottom = self.get_scroll_count(num_to_download)
        元素= driver.find_element_by_css_selector('div.-CX-私人AutoloadingPostsGrid__moreLoadingIndicator一个')
        driver.implicitly_wait(self.PAUSE)
        element.click()        y的范围内(INT(scroll_to_bottom)):
            self.scroll_page(驾驶员)    #负载毕竟个人资料照片,retur,源download_photos()
    time.sleep(1)
    来源= driver.page_source    #关闭Firefox窗口
    driver.close()    收益来源高清scroll_page(个体经营,司机):
    driver.execute_script(window.scrollTo(0,document.body.scrollHeight);)
    time.sleep(0.2)
    driver.execute_script(window.scrollTo(0,0);)高清get_scroll_count(个体经营,算):
    回报(INT(计数) - 24)/ 12 + 1高清的validate(个体经营):
    
    如果Instagram的用户名是有效返回True
    
    REQ = requests.get(self.profile_url)    尝试:
        req.raise_for_status()
    除:
        self.log(用户,self.username,'无效。,
                 级别= logging.ERROR)
        返回False    如果没有self.num_posts:
        self.log(用户,self.username,没有照片下载。,
                 级别= logging.ERROR)
        返回False
    返回True高清save_photo(个体经营,PHOTO_URL,PHOTO_NAME):
    image_request = requests.get(PHOTO_URL,标题= self.headers)
    = IMAGE_DATA image_request.content
    开放(PHOTO_NAME,世行)为FP:
        fp.write(IMAGE_DATA)    如果最后一次修改,在image_request.headers:
        modtime = calendar.timegm(eut.parsedate(image_request.headers [上次修改]))
        os.utime(PHOTO_NAME,(modtime,modtime))高清download_photos(个体经营):
    
    由于源$ C ​​$ C加载的页面的Instagram,
    提取所有的HREF和下载全分辨率照片
    来源:Instagram的轮廓papge的HTML源代码code
    
    num_to_download = self.num_to_download或self.num_posts
    如果self.html_source是无:
        self.html_source = self.load_instagram()    #检查目录是否存在,如果没有,使它
    如果不是op.exists(self.directory):
        os.makedirs(self.directory)    对进度条#指数
    photos_saved = 0
    self.log(保存照片,self.directory)    链接=通过re.findall(r'src =[HTTPS] +:... [\\ / \\ W \\ .-] * .. [\\ / \\ W \\ .-] * .. [\\ / \\ W \\。 - ] * .. [\\ / \\ W \\ .-] JPG,self.html_source)    在链接的链接:
        PHOTO_URL =链接[5:]
        PHOTO_URL = photo_url.replace('\\\\','')
        PHOTO_URL =应用re.sub(R'/ S \\ D + X \\ D + /','/',PHOTO_URL)        分= urlparse.urlsplit(PHOTO_URL)
        PHOTO_NAME = op.join(self.directory,split.path.split(/)[ - 1])        #保存全分辨率照片如果新
        如果不是op.isfile(PHOTO_NAME):
            self.save_photo(PHOTO_URL,PHOTO_NAME)
            photos_saved + = 1
            self.log('下载的文件{} / {}({})。'格式(
                photos_saved,num_to_download,op.basename(PHOTO_NAME)))
        其他:
            self.log('跳过文件',PHOTO_NAME,因为它已经存在。')        如果photos_saved> = num_to_download:
            打破    self.log(拯救,photos_saved,文件,self.directory)
高清的main():
    #解析参数
    解析器= argparse.ArgumentParser(说明='InstaRaider')
    parser.add_argument(用户名,帮助='用户名的Instagram)
    parser.add_argument(目录,有利于='在哪里保存图像)
    parser.add_argument(' - N','--num到下载',
                        帮助='邮件数量下载,键入= INT)
    parser.add_argument(' - L','--log级',帮助=日志级别,默认值='信息')
    ARGS = parser.parse_args()
    用户名= args.username
    目录= op.expanduser(args.directory)    袭击者= InstaRaider(用户名,目录
                         num_to_download = args.num_to_download,
                         LOG_LEVEL = args.log_level)    如果不是raider.validate():
        返回    raider.download_photos()
如果__name__ =='__main__':
    主要()


解决方案

这不是一个Python异常消息。这是的命令行帮助输出

输出由 argparse 模块,此处配置产生

  DEF的main():
    #解析参数
    解析器= argparse.ArgumentParser(说明='InstaRaider')
    parser.add_argument(用户名,帮助='用户名的Instagram)
    parser.add_argument(目录,有利于='在哪里保存图像)
    parser.add_argument(' - N','--num到下载',
                        帮助='邮件数量下载,键入= INT)
    parser.add_argument(' - L','--log级',帮助=日志级别,默认值='信息')
    ARGS = parser.parse_args()

当下 parser.parse_args()就是所谓的命令行参数进行解析,以匹配上面的配置。

具体而言,用户名目录位置参数是的需要的:

  parser.add_argument(用户名,帮助='用户名的Instagram)
parser.add_argument(目录,有利于='在哪里保存图像)

您需要指定在命令行上这些当您运行该脚本:

  Google_Map.py some_instagram_username /路径/到/目录/至/保存/图片

其他的命令行选项的可选的,并开始与 - -

如果你不能从一个控制台或终端的命令行运行它,你可以通过在选项直接解析器:

 高清主(的argv =无):
    #解析参数
    解析器= argparse.ArgumentParser(说明='InstaRaider')
    parser.add_argument(用户名,帮助='用户名的Instagram)
    parser.add_argument(目录,有利于='在哪里保存图像)
    parser.add_argument(' - N','--num到下载',
                        帮助='邮件数量下载,键入= INT)
    parser.add_argument(' - L','--log级',帮助=日志级别,默认值='信息')
    ARGS = parser.parse_args(ARGV)
    #....主(['some_instagram_username','/路径/到/目录/至/保存/图像'])

现在的参数是通过的argv 可选功能参数传递,作为一个列表。

不过,而不是让的main()解析参数,你可以只直接使用 InstaRaider()

  =袭击者InstaRaider('some_instagram_username','/路径/到/目录/至/保存/图像)
raider.download_photos()

I have tried to correct the following error which basically is about the arguments for 'username' and 'directory'. I have tried all possible ways but no luck. Python does not state the line of code that the following error refers to:

usage: Google_Map.py [-h] [-n NUM_TO_DOWNLOAD] [-l LOG_LEVEL]
                     username directory
Google_Map.py: error: the following arguments are required: username, directory

Please see the code here:

def __init__(self, username, directory, num_to_download = 10,
             log_level='info'):
    self.username = username
    self.profile_url = self.get_url(username)
    self.directory = directory
    self.PAUSE = 1
    self.user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
    self.headers = {'User-Agent': self.user_agent}
    self.html_source = None
    self.log_level = getattr(logging, log_level.upper())
    self.setup_logging(self.log_level)
    self.set_num_posts(num_to_download)
    self.setup_webdriver()

def get_url(self, path):
    return urlparse.urljoin('https://instagram.com', path)

def set_num_posts(self, num_to_download=None):
    self.num_posts = int(self.get_posts_count(self.profile_url) or 0)
    self.num_to_download = num_to_download

def setup_logging(self, level=logging.INFO):
    self.logger = logging.getLogger('instaraider')
    self.logger.addHandler(logging.StreamHandler())
    self.logger.setLevel(logging.INFO)

def log(self, *strings, **kwargs):
    level = kwargs.pop('level', logging.INFO)
    self.logger.log(level, u' '.join(str(s) for s in strings))

def setup_webdriver(self):
    self.profile = webdriver.FirefoxProfile()
    self.profile.set_preference("general.useragent.override", self.user_agent)
    self.webdriver = webdriver.Firefox(self.profile)
    self.webdriver.set_window_size(480, 320)
    self.webdriver.set_window_position(800, 0)

def get_posts_count(self, url):
    """
    Given a url to Instagram profile, return number of photos posted
    """
    response = requests.get(url)
    counts_code = re.search(r'\"media":{"count":\d+', response.text)
    if not counts_code:
        return None
    return re.findall(r'\d+', counts_code.group())[0]

def log_in_user(self):
    driver = self.webdriver
    self.log('You need to login to access this profile.',
             'Redirecting you to the login page in the browser.',
             level=logging.WARN)
    driver.get(self.get_url('accounts/login/'))

    # Wait until user has been successfully logged in and redirceted
    # to his/her feed.
    WebDriverWait(driver, 60).until(
        expected_conditions.presence_of_element_located(
            (By.CSS_SELECTOR, '.-cx-PRIVATE-FeedPage__feed'),
        )
    )

    self.log('User successfully logged in.', level=logging.INFO)
    self.set_num_posts()  # Have to set this again
    driver.get(self.profile_url)

def load_instagram(self):
    """
    Using Selenium WebDriver, load Instagram page to get page source
    """
    self.log(self.username, 'has', self.num_posts, 'posts on Instagram.')
    if self.num_to_download is not None:
        self.log("The first", self.num_to_download, "of them will be downloaded.")

    num_to_download = self.num_to_download or self.num_posts
    driver = self.webdriver
    # load Instagram profile and wait for PAUSE
    self.log("Loading Instagram profile...")
    driver.get(self.profile_url)
    driver.implicitly_wait(self.PAUSE)

    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    try:
        el = driver.find_element_by_css_selector(
            '.-cx-PRIVATE-ProfilePage__advisoryMessageHeader'
        )
    except NoSuchElementException:
        pass
    else:
        self.log_in_user()

    if (num_to_download > 24):
        scroll_to_bottom = self.get_scroll_count(num_to_download)
        element = driver.find_element_by_css_selector('div.-cx-PRIVATE-AutoloadingPostsGrid__moreLoadingIndicator a')
        driver.implicitly_wait(self.PAUSE)
        element.click()

        for y in range(int(scroll_to_bottom)):
            self.scroll_page(driver)

    # After load all profile photos, retur, source to download_photos()
    time.sleep(1)
    source = driver.page_source

    # close Firefox window
    driver.close()

    return source

def scroll_page(self, driver):
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    time.sleep(0.2)
    driver.execute_script("window.scrollTo(0, 0);")

def get_scroll_count(self, count):
    return (int(count) - 24) / 12 + 1

def validate(self):
    """
    returns True if Instagram username is valid
    """
    req = requests.get(self.profile_url)

    try:
        req.raise_for_status()
    except:
        self.log('User', self.username, 'is not valid.',
                 level=logging.ERROR)
        return False

    if not self.num_posts:
        self.log('User', self.username, 'has no photos to download.',
                 level=logging.ERROR)
        return False
    return True

def save_photo(self, photo_url, photo_name):
    image_request = requests.get(photo_url, headers=self.headers)
    image_data = image_request.content
    with open(photo_name, 'wb') as fp:
        fp.write(image_data)

    if "last-modified" in image_request.headers:
        modtime = calendar.timegm(eut.parsedate(image_request.headers["last-modified"]))
        os.utime(photo_name, (modtime, modtime))

def download_photos(self):
    """
    Given source code for loaded Instagram page,
    extract all hrefs and download full-resolution photos
    source: HTML source code of Instagram profile papge
    """
    num_to_download = self.num_to_download or self.num_posts
    if self.html_source is None:
        self.html_source = self.load_instagram()

    # check if directory exists, if not, make it
    if not op.exists(self.directory):
        os.makedirs(self.directory)

    # index for progress bar
    photos_saved = 0
    self.log("Saving photos to", self.directory)

    links = re.findall(r'src="[https]+:...[\/\w \.-]*..[\/\w \.-]*..[\/\w \.-]*..[\/\w \.-].jpg', self.html_source)

    for link in links:
        photo_url = link[5:]
        photo_url = photo_url.replace('\\', '')
        photo_url = re.sub(r'/s\d+x\d+/', '/', photo_url)

        split = urlparse.urlsplit(photo_url)
        photo_name = op.join(self.directory, split.path.split("/")[-1])

        # save full-resolution photo if its new
        if not op.isfile(photo_name):
            self.save_photo(photo_url, photo_name)
            photos_saved += 1
            self.log('Downloaded file {}/{} ({}).'.format(
                photos_saved, num_to_download, op.basename(photo_name)))
        else:
            self.log('Skipping file', photo_name, 'as it already exists.')

        if photos_saved >= num_to_download:
            break

    self.log('Saved', photos_saved, 'files to', self.directory)


def main():
    # parse arguments
    parser = argparse.ArgumentParser(description='InstaRaider')
    parser.add_argument('username', help='Instagram username')
    parser.add_argument('directory', help='Where to save the images')
    parser.add_argument('-n', '--num-to-download',
                        help='Number of posts to download', type=int)
    parser.add_argument('-l', '--log-level', help="Log level", default='info')
    args = parser.parse_args()
    username = args.username
    directory = op.expanduser(args.directory)

    raider = InstaRaider(username, directory,
                         num_to_download=args.num_to_download,
                         log_level=args.log_level)

    if not raider.validate():
        return

    raider.download_photos()


if __name__ == '__main__':
    main()

解决方案

That's not a Python exception message. That's command line help output.

The output is generated by the argparse module, configured here:

def main():
    # parse arguments
    parser = argparse.ArgumentParser(description='InstaRaider')
    parser.add_argument('username', help='Instagram username')
    parser.add_argument('directory', help='Where to save the images')
    parser.add_argument('-n', '--num-to-download',
                        help='Number of posts to download', type=int)
    parser.add_argument('-l', '--log-level', help="Log level", default='info')
    args = parser.parse_args()

The moment parser.parse_args() is called your command line arguments are parsed to match the above configuration.

Specifically, the username and directory positional arguments are required:

parser.add_argument('username', help='Instagram username')
parser.add_argument('directory', help='Where to save the images')

You'll need to specify these on the command line when you run the script:

Google_Map.py some_instagram_username /path/to/directory/to/save/images

The other command line options are optional and start with - or --.

If you can't run this from a console or terminal command line, you could pass in the options to the parser directly:

def main(argv=None):
    # parse arguments
    parser = argparse.ArgumentParser(description='InstaRaider')
    parser.add_argument('username', help='Instagram username')
    parser.add_argument('directory', help='Where to save the images')
    parser.add_argument('-n', '--num-to-download',
                        help='Number of posts to download', type=int)
    parser.add_argument('-l', '--log-level', help="Log level", default='info')
    args = parser.parse_args(argv)
    # ....

main(['some_instagram_username', '/path/to/directory/to/save/images'])

Now the arguments are passed in via the argv optional function parameter, as a list.

However, rather than have main() parse arguments, you could just use the InstaRaider() class directly:

raider = InstaRaider('some_instagram_username', '/path/to/directory/to/save/images')
raider.download_photos()

这篇关于Python不说明code线的一个错误是指的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆